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

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


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.


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


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