Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
120 views
in Technique[技术] by (71.8m points)

c# - Stop Entity Framework from modifying database

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?

question from:https://stackoverflow.com/questions/9230053/stop-entity-framework-from-modifying-database

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

See http://www.entityframeworktutorial.net/code-first/turn-off-database-initialization-in-code-first.aspx


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...