Migration not working as I wish… Asp.net EntityFramework

后端 未结 4 1020
攒了一身酷
攒了一身酷 2020-12-12 06:58

I was studying this tutorial asp.net Movie Tutorial and I have 2 simple files in my model.

 public class Movie
{
    public int ID { get; set; }

    [Requ         


        
4条回答
  •  庸人自扰
    2020-12-12 07:18

    Entity Framework will only migrate one DbContext. Period. There's no way around this, currently. That means very plainly and simply that everything that EF will need to work with must be in this single context.

    However, that doesn't preclude you from using other contexts elsewhere for different purposes. So in your MovieDBContext go ahead and add your UserProfile entity set:

    public DbSet User { get; set; }
    

    Then, in your other contexts, you just need to 1) make sure they interact with the same database and 2) that they don't have an initialization strategy, e.g.:

    public class AccountsContext : DbContext
    {
        public AccountsContext()
            : base("name=ConnectionStringNameHere")
        {
            Database.SetInitializer(null);
        }
    
        public DbSet User { get; set; }
        ...
    }
    

    That will make EF treat this context as a mapping to an existing database, so it won't try to create database tables, migrations, etc. All that will be done via your primary context (MovieDbContext in this case).

提交回复
热议问题