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

42,783

Solution 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.

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

Solution 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. =)

Solution 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;"

Solution 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.

Share:
42,783
Admin
Author by

Admin

Updated on October 02, 2020

Comments

  • Admin
    Admin over 3 years

    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