EF Code First from Database - Already an object in the database

c# entity-framework entity-framework-6

Question

I created a new .NET project and added ADO.NET EF 6.
I went through the EF wizard and choose Code First from Database.
Then I selected a table. Let's call it "Product".
This created "public partial class Product" and "public partial class Model1".

I immediately created a LINQ query in my application to query the "Product" but I get the following error.

There is already an object named 'Product' in the database.

When I run SQL Profile I see the following:

CREATE TABLE [dbo].[Product] ...

I don't understand why the project is trying to create the table since the table already exists.

I read a couple of articles telling me I need to enable migrations but I really don't want my project to be able to create tables in the database.
(We have a DBA that does not give us access to create tables "easily" in the database)

I then decided to try creating a "Migrations" folder and a "internal sealed class Configuration" with the following:

public Configuration()
{
    AutomaticMigrationsEnabled = false;
    AutomaticMigrationDataLossAllowed = false;
}

This gives me a new error.

Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration.

How to I fix this?

UPDATE
So I created both a VB.NET and a C# application with the exact same code pointing against the same database.
The C# version has the "already in database" issue.
The VB.NET version does not have the "already in database" issue.

1
2
11/12/2015 2:21:55 PM

Accepted Answer

I'm not sure why but in VB.NET the Code First from Database works fine but in C# this does not work correctly for EF6.

I decide to let C# create the "dbo.__MigrationHistory" and found that this worked great until I added a new class that was the same name as a table in the database.
This caused the following error:

The model backing the 'TEST' context has changed since the database was created. Either manually delete/update the database, or call Database.SetInitializer with an IDatabaseInitializer instance.

Again I thought why is EF requiring migration information when I already have a database and was trying to use Code First "FROM" Database.

So a quick StackOverFlow search lead me to the following article:
"Entity Framework Code Only error: the model backing the context has changed since the database was created"
Once I added the following line of code in the Global.asax then all started working correctly.

Database.SetInitializer<YourContext>(null);
0
5/23/2017 12:06:43 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow