Already an object in the database

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.


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);

You have an existing database and you have a DBA who takes care of it for you. You need to change to Entity Framework Database First Approach. This allow you to work with existing database without making modification to the database referential integrity. You need to look into Database First Approach. Quick Intro here https://msdn.microsoft.com/en-us/data/jj591506.aspx


There is a step by step guide for this approach here https://msdn.microsoft.com/en-us/library/jj200620.aspx. Please check your steps.

It looks like a duplicate of this and this. There are tons more on this subject.

链接地址: http://www.djcxy.com/p/90286.html

上一篇: EF先用现有表格代码

下一篇: 已经是数据库中的一个对象