Reputation: 25521
I'm starting to play around with the code-first approach to the entity framework, primarily so that I can decorate my properties with annotations for display in my view (otherwise, right now I have to create a class that is nearly identical to the one that entity framework generated for me just so I can add annotations, and then copy the data from one object to the next).
Right now it looks like when I start my application it is trying to create a database.
I do not want entity framework to ever modify my database. No. Not ever. Don't even try it. It really isn't that hard to modify databases; I would feel much more comfortable if I did that myself. I don't need a framework to hold my hand when designing a database.
Can I tell the framework to stop trying to modify my database? I'm very hesitant to use code-first now as the fact that it's trying to modify my database is rather frightening. Even in development I never want to see it happen.
Am I out of luck?
Upvotes: 43
Views: 20414
Reputation: 29
I realize this thread is old but hopefully the OP found the answer. If not...
Have Visual Studio generate the EF6 entity classes from the existing database:
https://www.entityframeworktutorial.net/code-first/code-first-from-existing-database.aspx
and then add (as already said):
Database.SetInitializer<SchoolDBContext>(null);
to stop EF6 from messing with the database.
Note that a potential issue with the auto-generated classes is the resultant code uses fluent.
Upvotes: -1
Reputation: 43067
If you don't want EF to create your database, you can disable the database initializer:
public class SchoolDBContext: DbContext
{
public SchoolDBContext() : base("SchoolDBConnectionString")
{
//Disable initializer
Database.SetInitializer<SchoolDBContext>(null);
}
public DbSet<Student> Students { get; set; }
public DbSet<Standard> Standards { get; set; }
}
Upvotes: 66
Reputation: 4422
If you want to use EF but never modify the database then you probably don't want code first. You probably want something more like database first.
http://blogs.msdn.com/b/adonet/archive/2011/03/15/ef-4-1-model-amp-database-first-walkthrough.aspx
Links from answer comments:
http://automapper.codeplex.com/
Getting Started with AutoMapper
edit: I misunderstood the goal, you should reference this answer where the following correct code was given:
If you don't want EF to create your database, you can disable the database initializer
Database.SetInitializer<MyContext>(null);
Upvotes: 4
Reputation: 18760
When you declare you're initialiser, use the base class:
public class DatabaseInitialiser : CreateDatabaseIfNotExists<MyContext>
rather than :
public class DatabaseInitialiser : RecreateDatabaseIfModelChanges<MyContext>
Or if you use :
Database.SetInitializer<MyContext>(new RecreateDatabaseIfModelChanges<MyContext>);
replace this with :
Database.SetInitializer<MyContext>(new CreateDatabaseIfNotExists<MyContext>);
Upvotes: 2
Reputation: 151588
Since code first pretty much exactly does what you describe, I do not understand your question.
If you don't want EF to fiddle with your database, then generate a model from your existing database.
Upvotes: 1