I have a MVC 4 project set up and generated all the model classes using Entity Framework. Then I added a class and named it same as "MyProjectEntities" class and made it partial so I can add personalized methods in that class. I added bunch of methods to query database in this class and it builds fine. When I call these methods though I get UnintentionalCodeFirstException. I'm not sure what did I do wrong?
--- Tried several versions of code in the web.config file in entity framework section with no luck. That exception is usually thrown when your connection string doesn't include the "metadata" stuff. Are you sure you haven't changed the connection string on the server?
The class that derives DbContext is called context class in entity framework. DbContext is an important class in Entity Framework API. It is a bridge between your domain or entity classes and the database.
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.
The OnModelCreating method allows us to configure the model using DbModelBuilder Fluent API in EF 6. Gets an DbEntityEntry for the given entity. The entry provides access to change tracking information and operations for the entity. Executes INSERT, UPDATE and DELETE commands to the database for the entities with Added, Modified and Deleted state.
You need to provide the correct connection string. Not just the normal "Data Source=" type strings.
You will have a connection string that starts with "metadata=" in your config file, use that one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With