I have an error:
An exception of type 'System.Data.Entity.Infrastructure.UnintentionalCodeFirstException' occurred in DataAccess.dll but was not handled in user code
Additional information: The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development. This will not work correctly. To fix this problem do not remove the line of code that throws this exception. If you wish to use Database First or Model First, then make sure that the Entity Framework connection string is included in the app.config or web.config of the start-up project. If you are creating your own DbConnection, then make sure that it is an EntityConnection and not some other type of DbConnection, and that you pass it to one of the base DbContext constructors that take a DbConnection.
In DataAccess project I have an EF 6 with App.Config file with string:
<connectionStrings> <add name="CVJobOnlineEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model 1.msl;provider=System.Data.SqlClient;provider connection string="data source=STEFAN-PC\SQLEXPRESS;initial catalog=CVJobOnline;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" /> </connectionStrings>
and in my second project, which is the main Start-Up project I have in WebConfig:
<connectionStrings>
<add name="CVJobOnlineEntities"
providerName="System.Data.SqlClient"
connectionString="Server=.\SQLEXPRESS;Database=CVJobOnline;Integrated Security=True;"/>
So, obviously I am mixing EDMX and CodeFirst conn strings, but, I need it CodeFirst because of my Identity tables which I was incorporate in my SQL SERVER DB.
Also in my DbContext, I recalled base to use FirstCode (Model1.Context.cs):
public partial class CVJobOnlineEntities : DbContext
{
public CVJobOnlineEntities()
: base("name=CVJobOnlineEntities")
{
}
You must specify your connection string only once at the entry point of your application. Your DataAccess project does not need a connection string if it is not executable. Cut & paste the connection string from your DataAccess project to the web configuration file of your application entry point, overwriting the old one.
The problem was not exactly mixing two types of connection strings, since the one from DataAccess was never read by the Entity Framework. The one provided at your entry point config was just wrong in your scenario, because you are using model-first and not code-first.
Related
I have 3 different project having their respective EF entity data model pointing to same database.I don't want to save connection string in each of these project's app.config file but want to share it between my models.
I see this link on stackoverflow How to share a connection string between multiple entity data model.
But the problem with it is if I will update the EF model it will overwrite the code in EF Model's context and it will inherit from DbContext not from BaseContext.
Please help how can I resolve this.
You have to move your connection string in a separate config file:
ConnectionStrings.config
<?xml version="1.0" encoding="utf-8" ?>
<connectionStrings>
<add name="connectionString"
connectionString="Integrated Security=SSPI; Persist Security Info=False; Initial Catalog=DbName; Data Source=.\SQLExpress;"
providerName="System.Data.SqlClient" />
</connectionStrings>
Modify the connection string so that fit your requirement.
Then you can share it with all your projects like that:
1) Open your App.config (This file found in your project)
2) Add this line code somewhere behind </configSections>
...
<connectionStrings configSource="ConnectionStrings.config"/>
...
The trick in configSource:
"Gets or sets the name of the include file in which the associated configuration section is defined, if such a file exists."
https://msdn.microsoft.com/en-us/library/system.configuration.sectioninformation.configsource(v=vs.110).aspx
What will happened:
ConnectionStrings.config must be first copied
All YourApplicationName.config will reference the shared connection string config file.
If the project does not have any App.config then just add it! or you can also loaded manually with ConfigurationSettings.
This is the best way to share the database configuration between the app.configs and when you change for example the Sql Server name, then you have only to modify the ConnectionStrings.config and not all App.configs!
It resolved as connection string always picked from MVC project and all other class library projects are referencing it automatically.
I'm new here and in Entity Framework too! i have a problem!
This is my Connection string in web.config:
<add name="SolutionName.DALClassLibrary.Setting.ShConnectionString"
connectionString="Data Source=(localdb)\v11.0;AttachDbFilename=|DataDirectory|ASH.mdf; Database=ASH.mdf; Initial Catalog=ASH;Integrated Security=True;User Instance = true ; MultipleActiveResultSets=True"
providerName="System.Data.SqlClient" />
And i wrote this ConnectionString in App.config in DAL project and setting of project too.
This is my context constructor:
public ASHContext(): base("ShConnectionString")
{
Database.SetInitializer<ASHContext>(new MigrateDatabaseToLatestVersion<ASHContext, Migrations.Configuration>());
}
When i run the project in App_Data it makes a new Database with ShConnectionString.mdf name. But i have ASH.mdf database in App_Data already.
It can't find connectionstring or ConnectionString is wrong? or the problem in somewhere else?
Sorry for my poor English language.
You're passing the wrong name to DBContext. Either use ShConnectionString as name in your configuration settings or change the name for DBContext to the one used in configuration.
I use SQL Server developer edition and would like to use EF code first. I found many articles explaining how to work with either a localdb or SQLExpress. How do I tell my project to rather use my ..\SQL2008 instance?
I'm thinking that somewhere, somehow, one must be able to tell the project to use a specific connectionstring. But where? Adding it to my app.config file doesn't work. This is what I've tried:
<connectionStrings>
<add name="Context" connectionString="Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=TimeApp;Data Source=Amanda-PC\SQL2008; MultipleActiveResultSets=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
You can specify which connection string to use by passing the name of the connection string to the DbContext.
public class YourContext : DbContext
{
public YourContext()
: base("Context")
{
}
}
See this for more information
I am using VS2012 with .NET 4.5. I am learning Entity Framework and I have a problem. I added ADO.NET Entity Data Model to my project and generated my entities using the wizard.
The wizard added a connection string into app.config file :
<add name="MalariaEntities" connectionString="metadata=res://*/MalariaEntities.csdl|res://*/MalariaEntities.ssdl|res://*/MalariaEntities.msl;provider=System.Data.SqlClient;provider connection string="data source=OFIR-PC;initial catalog=Malaria;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
In my project I am seeing all the entities and it seems fine but when I try to do any database action I get the following error:
No connection string named 'MalariaEntities' could be found in the application config file.
For example, I tried to insert simple row:
using (MalariaEntities DB_Context = new MalariaEntities())
{
MapsMainCategoriesDsc a = new MapsMainCategoriesDsc();
a.Category = "aa";
DB_Context.MapsMainCategoriesDsc.Add(a);
DB_Context.SaveChanges();
}
When the debugger reach to the last line the exception is raised. What can be the problem?
The wizard added a connection string so why my project cannot use it?
Thanks
I have a EF connection string in an MVC project like so:
connectionString="metadata=res:///Models.db.csdl|res:///Models.db.ssdl|res://*/Models.db.msl;provider=System.Data.SqlClient;provider connection string="Data Source=localhost;Initial Catalog=SystemName;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient"
All is well until I added a second project and referenced the MVC project that contains the edmx, but during runtime I get:
The specified metadata path is not valid. A valid path must be either an existing directory, an existing file with extension '.csdl', '.ssdl', or '.msl', or a URI that identifies an embedded resource.
I've read post after post, but I can't figure out how to correctly reference the metadata in the MVC project. Can someone point me in the correct direction? I don't want to create a connection string that is so specific it breaks during deployment and debug.
The format for resources is:
Metadata=res://<assemblyFullName>/<resourceName>.
The lazy way is to use a wild card res://*/bah.msl. Which would load the model/mapping files from the bin directory, calling assembly, as well as referenced assemblies.
In your case:
res:///Models.db.csdl|res:///Models.db.ssdl|res://*/Models.db.msl
Is incorrect, try:
res://*/Models.db.csdl|res://*/Models.db.ssdl|res://*/Models.db.msl
Complete string:
connectionString="metadata=res://*/Models.db.csdl|res://*/Models.db.ssdl|res://*/Models.db.msl;provider=System.Data.SqlClient;provider connection string='Data Source=localhost;Initial Catalog=SystemName;Integrated Security=True;MultipleActiveResultSets=True'" providerName="System.Data.EntityClient"
Alternatively you could use absolute references which is faster(but I am assuming this will be much more painful for you):
Metadata=res://<DLL>, <Version>, neutral, <SN>/Models.db.csdl|Models.db.ssdl|Models.db.msl