EF 6在不同的DbContext之间共享同一实体吗? [英] EF 6 share the same entity between different DbContexts?

查看:171
本文介绍了EF 6在不同的DbContext之间共享同一实体吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有EF 6的ASP.NET MVC5。
我试图遵循DDD模式,并且具有IdentityContext和AddressContext。

I am using ASP.NET MVC 5 with EF 6. I am trying to follow DDD patern and I have IdentityContext and AddressContext.

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

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

}

public class AddressContext: DbContext
{
    public AddressContext(): base("DefaultConntection"){}

    public DbSet<Location> Locations { get; set; }
}

当我尝试扩展(添加迁移和更新数据库)时我属于IdentityContext的ApplicationUser,我收到数据库中已经有一个名为'Locations'的对象错误。

When I am trying to extend(add-migration and update-database) my ApplicationUser which belongs to IdentityContext, I am getting "There is already an object named 'Locations' in the database" error.

public class ApplicationUser : IdentityUser
{
    public virtual Nullable<int> LocationId { get; set; }

    public virtual Location Location { get; set; }
}

如何在IdentityContext和AddressContext之间共享位置实体?

How could I share Location Entity between IdentityContext and AddressContext?

任何帮助将不胜感激。

推荐答案

解决方案是只有一个包含所有DbSet的上下文,然后仅使用它来更新数据库(什么也没有),然后关闭其他每个上下文的数据库初始化。您可以通过在业务上下文的构造函数中进行设置来实现:示例:

A solution would be to have a single context, that contains all of your DbSets, and then just use that to update your database(and nothing else), and then turn off database initialization for each of the other contexts. You can do this by setting it in the constructor of your business contexts:. Example:

public class IdentityContext : IdentityDbContext<ApplicationUser>
{
        public IdentityContext()
            : base("DefaultConntection", throwIfV1Schema: false)
    {
        Database.SetInitializer<IdentityContext>(null);
    }

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

}

public class AddressContext: DbContext
{
    public AddressContext(): base("DefaultConntection")
    {
        Database.SetInitializer<AddressContext>(null);
    }

    public DbSet<Location> Locations { get; set; }
}


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

    public DbSet<Location> Locations { get; set; }
    //Additional DbSets here...
}

,迁移上下文继承自 IdentityDbContext< ApplicationUser> ,因此它将包含您的所有Identity内容。处理初始化的一种更好的方法可能是定义一个已关闭它的BaseContext类,然后仅从该基础上下文继承,如下所述: http://msdn.microsoft.com/zh-cn/magazine/jj883952.aspx 。有关更多信息,请参见此链接以获取类似问题:实体框架:一个数据库,多个DbContext。这是个坏主意吗?

In this example, the migration context is inheriting from IdentityDbContext<ApplicationUser>, so that it'll include all of your Identity stuff. A better way to handle the initialization, might be to defining a BaseContext class, that has it turned off, and then just inheriting from that base context, as described here:http://msdn.microsoft.com/en-us/magazine/jj883952.aspx. See this link for a similar question with more info: Entity Framework: One Database, Multiple DbContexts. Is this a bad idea?

这篇关于EF 6在不同的DbContext之间共享同一实体吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆