How to use different Entity Framework DbContext in ASP.NET MVC application scope?

I am currently building a web application with first MVC 4 code script and Entity Framework.

In the main structure of my application, I have Dbcontext(BlogDB)

to manage some classes Blog

. It works great as it creates all the tables I need in the database. Then I created a zone to host the online store. My idea is to create a split class DbContext

( OnlineStoreDB

) to handle classes used only for the online store.

My problem after startup OnlineStoreDB

, entity framework not only created tables for OnlineStore

, BUT ALSO dropped old tables.

My questions:

  • If you know a way to keep old tables?
  • How to accurately manage multiple EF context classes in a single application?

Code:

public class BlogDB : DbContext
{
    public BlogDB ()
        : base("DBConnection")
    {
        Database.SetInitializer(new BlogInitializer());
    }

    public DbSet<Blog> Blogs { get; set; }
    public DbSet<Author> Authors { get; set; }
    public DbSet<Comment> Comments { get; set; }
}

public class OnlineStoreDB : DbContext
{
    public OnlineStoreDB() :
        base("DbConnection")
    {
        Database.SetInitializer(new OnlineStoreInitializer());
    }

    public DbSet<Order> Orders { get; set; }
    public DbSet<Product> Products { get; set; }
    public DbSet<User> Users { get; set; }
}

      

+3


source to share


2 answers


Xavier, welcome to the first code!

Yes, the first code is a brilliant approach that promised a lot. But now you are trapped. There is no smart mind at Microsoft (or beyond as far as I know) that has come up with a fluid way to intelligently modify tables without jeopardizing the data and possibly the schema.

2 years ago, the implementation strategy was to dump and rebuild the database. It was simply unbearable as many of us did not have access to SU and were stopped on our roads.

For all the benefits I've found from code, I prefer DB first. Although the data cannot be easily persisted, annotations can be done through friend classes.

Microsoft has come up with some clever migration strategies. I highly recommend you read both articles. Code Project 2nd:



1) http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-code-based-migrations-walkthrough.aspx

2) http://www.codeproject.com/Articles/504720/EntityplusFrameworkplusCodeplusFirstplusMigrations

whether you decide to continue with Code-First, they should be enlightening. I sound like a critic, but I'm torn between the benefits of one and the stability of the other.

Finally, I don't think you should keep 2 dbcontexts. Your POCOs must be combined in 1 context.

+3


source


If you want the tables not to change, you need to set the initializer to null in both DBC contexts if they have a subset of tables.



But I see no point in creating two DBC contexts for the same database. Can you clearly separate the two sets of tables (Domains) in your database?

+1


source







All Articles