在2个DbContexts中定义模型

时间:2014-04-30 14:02:20

标签: c# asp.net-mvc entity-framework entity-framework-6

我有2个DbContexts,一个管理IdentityUser和其他模型 课程MyUserApplicationUser

具有一对一的关系
public class ApplicationUser : IdentityUser
{
    public virtual MyUser  MyUser { get; set; }

}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }

    public DbSet<MyUser> MyUsers { get; set; }
}

在模型项目中,我有一个名为Review的班级,其中评论与MyUser

有多对一
 public class Review
{
    public int Id { get; set; }
    public int MyUserId { get; set; }
    public virtual MyUser MyUser { get; set; }
}


public class MyUser
    {
        [Key, ForeignKey("ApplicationUser")]
        public string ApplicationUserId { get; set; }

        public virtual ApplicationUser AppUser { get; set; }

        public ICollection<Review> Reviews { get; set; }

    }

public class MyContext: DbContext
    {
        public MyContext() :
            base(nameOrConnectionString: "DefaultConnection")
        {
        }

        public DbSet<Review> Reviews { get; set;}

        // Do i have to add MyUser DebSet here?

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {

            base.OnModelCreating(modelBuilder);
        }
    }

我的问题是,MyUser应与评论和ApplicationUser相关联 评论在MyContext中被定义,ApplicationUser中的ApplicationDbContext被定义为<{1}}

我怎么写关系?在whitch DbContext中,有什么想法吗?

0 个答案:

没有答案