2

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.

goroth
  • 2,510
  • 5
  • 35
  • 66
  • Have you tried executing these two commands on your package manager console? "add-migration" then "update-database". [This](https://msdn.microsoft.com/en-au/data/jj591621.aspx) may help you. – Kosala W Nov 11 '15 at 22:44
  • @Kosala Yes I have and those commands tried to create a table in the database called "dbo.__MigrationHistory". I'm trying to use Code First from Database without adding more tables into the database. – goroth Nov 12 '15 at 13:59

3 Answers3

0

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);
Community
  • 1
  • 1
goroth
  • 2,510
  • 5
  • 35
  • 66
-1

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

Julius Depulla
  • 1,493
  • 1
  • 12
  • 27
  • Microsoft is requiring Code First type approach in EF 7 (no designer for database) and they already have EF 6 Code First from Database so I would like to try and use that if possible. – goroth Nov 12 '15 at 13:48
-1

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.

Community
  • 1
  • 1
Matija Gobec
  • 850
  • 6
  • 12
  • The Microsoft guide tells you how to use EF Code First from Database which I am already doing. The guide does not tell the user about the "migration" problems I am having. The second link you listed is not a duplication because that link is for EF Code First without existing Database. The thrid link uses "update-database" which I am trying not to do. DBA does not want me inserting tables in the database if possible. – goroth Nov 12 '15 at 13:45