【问题标题】:entity framework two project with share database实体框架二项目与共享数据库
【发布时间】:2016-01-13 22:59:49
【问题描述】:

我有两个完全不同的项目站点 喜欢:

adminPanel(asp mvc site),utilities(classLibrary),mobileConnection(asp web api)

adminPanel 项目和 MobileConnection 项目有单独的身份验证,但他们的帐户有一些共享字段,

实用程序代码(与两个项目共享):

public class MobileApplicationUser : IdentityUser
{
    public virtual Profile Profile { get; set; }

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<MobileApplicationUser> manager, string authenticationType)
    {       
        var userIdentity = await manager.CreateIdentityAsync(this, authenticationType);
        return userIdentity;
    }
}

public class AdminApplicationUser : IdentityUser
{
    public virtual Organization organization { get; set; }

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<AdminApplicationUser> manager, string authenticationType)
    {         
        var userIdentity = await manager.CreateIdentityAsync(this, authenticationType);
        return userIdentity;
    }

}

管理项目代码:

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

    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<AdminApplicationUser>().ToTable("AdminUser");
        modelBuilder.Entity<IdentityRole>().ToTable("AdminRole");
        modelBuilder.Entity<IdentityUserRole>().ToTable("AdminUserRole");
        modelBuilder.Entity<IdentityUserClaim>().ToTable("AdminUserClaim");
        modelBuilder.Entity<IdentityUserLogin>().ToTable("AdminUserLogin");
    }

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

    public DbSet<Profile> Profiles { get; set; }
    public DbSet<Competition> Competitions { get; set; }
    public DbSet<Message> Messages { get; set; }
    public DbSet<Organization> Organizations { get; set; }

}

mobileConnection 项目代码:

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

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<AdminApplicationUser>().ToTable("MobileUser");
        modelBuilder.Entity<IdentityRole>().ToTable("MobileRole");
        modelBuilder.Entity<IdentityUserRole>().ToTable("MobileUserRole");
        modelBuilder.Entity<IdentityUserClaim>().ToTable("MobileUserClaim");
        modelBuilder.Entity<IdentityUserLogin>().ToTable("MobileUserLogin");
    }

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

    public DbSet<Profile> Profiles { get; set; }
    public DbSet<Competition> Competitions { get; set; }
    public DbSet<Message> Messages { get; set; }
    public DbSet<Organization> Organizations { get; set; }

}

我的个人资料和组织有关系, 所以...我不能同时更新数据库项目并有我的目标! 我该怎么办?

【问题讨论】:

  • 我不想拥有一个 DbContext 并将我的授权与角色和用户分开
  • 你的目标不明确。最终状态是什么?
  • 我想要一个共享数据库和两个 ASP MVC Web 应用程序(他们有单独的身份验证)
  • 您展示的代码与该目标有什么关系?

标签: asp.net-mvc entity-framework authentication asp.net-identity dbcontext


【解决方案1】:

假设这是一个代码优先问题。 是的,您可以让 1 个 DB 上下文访问多个数据库,或者让多个 DBContext 访问 1 个数据库。管理迁移是关键挑战。

有界 DBContext 和实体框架

是您开始研究的好地方

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多