Entity Framework: The context is being used in Cod

2020-06-07 06:29发布

问题:

I am developing an WPF application with EF 6 database first approach, I am have 1 project in my solutions, if i run my project this error always appear.

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. To learn more about Code First, Database First, and Model First see the Entity Framework documentation here: http://go.microsoft.com/fwlink/?LinkId=394715

回答1:

My mistake was using standard connection string in constructor

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

but Entity Framework needs 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)

Edit: Changed code to be formatted as code so it's easier to read.



回答2:

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

E.g. if your 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" />

Prepend 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" />


回答3:

One thing you can do is... (if is Database first)

Open the .edmx[Diagram] -> right click -> "Update Model from database"

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

If doesn't... probably your connection is broken and the dialog for VS creates a new connection string will appear instead. =)



回答4:

You shouldnt use generated connection string, now you have all metadata files included in your solution. Instead try use in connection string section of app.config:

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


回答5:

Very much late but still helpful. I got stuck in a similar problem. Posted a question about on SO and was able to find a solution. You can refer to Connection String errors in C# Web Api. My situation was I had two connection strings in web.config (you'll get to know why when go to the link). Commenting one string was raising the error you got while commenting the other one was raising error as below:

An error occurred when trying to create a controller of type 'AccountController'. Make sure that the controller has a parameterless public constructor.

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



回答6:

I also faced this exact same message, but workig with a web MVC project. This message is fired when I try to auto generate the Controller from the imported model. It seems that it is not working because "that was generated from an EDMX file".

The good news is that it works if I generate the model based in the "Code First" instead of "EF Designer". The bad news is that I can't use the EF Designer if I want that the automatically controller generation works. Does not matter which from those two ways you generates your model. Once the model is generated, you use it in the same way.

Tries to remove all your emdx objects from your project and recreate the model based in the Code First instead of EF Designer. Worked for me!



回答7:

I have two projects:

One is for the generated EDMX file and all related models.

The other one is the ASP.NET MVC Web.

I encountered this issue since the connection string that I am using on the ASP.NET MVC Web project is the normal string that I use using ADO.NET connection. So what I did is the following:

  • Open the app.config on your EDMX project files.

  • Copy its connection string.

  • Paste it on the WEB project since this will be used when you start the application.