Entity Framework: the context is used in Code First mode with the code that was generated from the EDMX file

I am developing a WPF application with the first EF 6 database approach, I have one project in my solutions, if I run my project, this error always appears.

The context is used in Code First mode with the code that was generated from the EDMX file for developing Database First or Model First. This will not work correctly. To resolve this issue, do not delete the line of code that throws this exception. If you want to use Database First or Model First, make sure that the Entity Framework connection string is included in the app.config or web.config of the startup project. If you create your own DbConnection, make sure it is an EntityConnection and not some other type of DbConnection, and pass it to one of the basic DbContext constructors that accept DbConnection. To learn more about Code First, Database First, and Model First, see the Entity Framework documentation here: http://go.microsoft.com/fwlink/?LinkId=394715

+12
source share
8 answers

My mistake was to use the standard connection string in the constructor

( Server = test\test; Database = DB; User Id = test_user;Password = test ),

but Entity Framework needs a different format

( metadata=res://*/DBModel.csdl|res://*/DBModel.ssdl|res://*/DBModel.msl;provider=System.Data.SqlClient;provider connection string="data source=test\test;initial catalog=DB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework""" providerName = ""System.Data.EntityClient )

Change: The code for formatting as code has been changed to make it easier to read.

+13
source

EF makes assumptions based on the presence or absence of a metadata section in the connection string. If you get this error, you can add the metadata section to the connection string in the configuration file.

eg. if the connection string looks like this:

  <add name="MyModel" connectionString="data source=SERVER\INSTANCE;initial catalog=MyModel;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" /> 

Prepare metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl; so that it looks like this:

 <add name="MyModel" connectionString="metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl;data source=SERVER\INSTANCE;initial catalog=MyModel;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" /> 
+9
source

One thing you can do is ... (if the database is first)

Open .edmx [Chart] → right-click → "Update Model from Database"

And see if the "Add", "Update" and "Delete" tabs appear.

If not ... perhaps your connection is broken and a dialog for VS appears instead, a new connection string is created. =)

+3
source

You should not use the generated connection string, now you have all the metadata files included in your solution. Instead, try using the app.config section in the connection string :

 "data source=localhost\sqlexpress; initial catalog=sample; integrated security=True;MultipleActiveResultSets=True;" 
+1
source

Very late, but still useful. I am stuck in a similar problem. Wrote a question about SO and was able to find a solution. You can refer to Connection String Errors in C # Web Api . My situation was that I had two connection strings in web.config (you will find out why when you go to the link). By commenting on one line, you raised the error you received, commenting on the other which raised the error, as shown below:

An error occurred while trying to create a controller of type AccountController. Make sure that the controller does not have a constructor without parameters.

what I did: I named my first connection string as DefaultConnection, and in the constructor of the ApplicationDbContext class I gave it DefaultConnection. Now my AccountController uses this connection string, and all other controllers use the second connection string. This solved my problem.

+1
source

I also came across the same post, but worked with an MVC web project. This message is triggered when I try to automatically generate a controller from an imported model. It does not seem to work because "it was created from an EDMX file."

The good news is that it works if I create a model based on "Code First" instead of "EF Designer". The bad news is that I cannot use EF Designer if I want the controller to work automatically. It does not matter which of these two methods you create your model. When a model is generated, you use it in the same way.

Attempts to remove all of your emdx objects from your project and recreate a model based on Code First instead of EF Designer. Worked for me!

0
source

I have two projects:

One for the generated EDMX file and all related models.

The other is ASP.NET MVC Web.

I ran into this problem because the connection string that I use in an ASP.NET MVC web project is the usual string that I use when using an ADO.NET connection. So, I did the following:

  • Open the app.config file in the EDMX project files.

  • Copy the connection string.

  • Insert it into the WEB project, as it will be used when the application starts.

0
source

Add metadata = "res: ///MyModel.csdl | res: ///MyModel.ssdl | res: //*/MyModel.msl" to connectionstring and providerName = "System.Data.EntityClient" will solve the problem

-2
source

All Articles