Take the 2-minute tour ×

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?

share|improve this question
it sounds like your model doesn't exactly match your database. You could try using a copy of your database and point EF at that. Let it create modify the db and compare the differences, then modify your model accordingly. You should also take a look at the EntityTypeConfiguration generic class. –  Antony Scott Feb 10 '12 at 15:24
Modify schema or modify data? –  jrummell Feb 10 '12 at 15:34
add comment

5 Answers

up vote 14 down vote accepted

If you don't want EF to create your database, you can disable the database initializer:

share|improve this answer
It would be more helpful if you could point out where to put the code. –  Rahat Jul 4 '13 at 18:11
You can put it in the static constructor of the context class, as in this example video: msdn.microsoft.com/en-us/data/jj572367 –  Christian Fredh Jul 11 '13 at 11:30
This should be the accepted answer. –  Luke Sampson Feb 2 at 6:13
add comment

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>);
share|improve this answer
add comment

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.

share|improve this answer
Right, but if I generate a model from my database, then I am missing the data annotations that I need for displaying data in my views. It results in a lot of duplicate work; essentially creating an identical class as the model that was generated and then decorating those properties with annotations. I am trying to avoid all the copying effort involved with that. –  Justin Helgerson Feb 10 '12 at 15:29
You can decorate generated classes by creating partial classes or buddy classes (MetadataAttribute). –  jrummell Feb 10 '12 at 15:38
add comment

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.


Links from answer comments:
Getting Started with AutoMapper

edit: I misunderstood the goal, you should reference this answer where the following correct code was given:


share|improve this answer
I agree that database first sounds like what I want, but I am trying to get around the issue of having to create a class that is nearly identical to the model that is generated for me. I have to create a nearly identical class so that I can decorate properties with annotations for display in my view (e.g. enforcing validation). It results in me having to loop through all of my "EF" objects and copy/map them to my new object that carries the annotations. –  Justin Helgerson Feb 10 '12 at 15:33
I would say that you should never be passing your db model objects to your views. Generally you create another class that is then passed to your view that only contains the properties that are available to that view. You would then decorate that class with your validation properties –  Anthony Shaw Feb 10 '12 at 15:37
@AnthonyShaw - I haven't seen many examples of people doing that, but that's exactly what I'm doing right now. I think I felt apprehensive about it because it seemed like a lot of mundane copying/mapping work. Is there a pattern that you use for turning your database model object into an object for display in your view? How do you usually do it? –  Justin Helgerson Feb 10 '12 at 15:39
@Ek0nomik then perhaps automapper is a solution. It can automagically copy properties from one class to another, given some restrictions (approximately same field names and so on). You still have to define the classes (your own models) however. –  CodeCaster Feb 10 '12 at 15:51
@Ek0nomik, like CodeCaster has suggested, I create the ViewModel, if you will, with the properties that I need for the specific view, and use AutoMapper (automapper.codeplex.com or available via NuGet) which uses reflection to map properties (as long as their names match). I originally thought it made for mundane duplication, but as I've continued to do it, I've found many cases where passing my actual object to the view caused issues. –  Anthony Shaw Feb 10 '12 at 16:39
add comment

Try configuring exactly how your model interacts with the database by looking at the EntityTypeOnfiguration generic class. I have written a blog post on how to automatically register your configuration classes, so that might be of some use to you.

The EntityTypeConfiguration class allows you to tell EF what columns/table names to use and other stuff like foreign key relationships. Just call the Property/ToTable/HasXXX methods in the constructor of your derived EntityTypeConfiguration class

public class MyConfig : EntityTypeConfiguration<MyModel>
    public MyConfig()
        Property(x => x.FieldName).HasColumnName("actual_field_name");


Note: the above code was written on my iPad without visual studio in sight!!

share|improve this answer
add comment

Your Answer


