【问题标题】:Entity Framework 6.0, ASP.Net Identity 2.0, Multiple Migrations, Related ClassesEntity Framework 6.0、ASP.Net Identity 2.0、多次迁移、相关类
【发布时间】:2014-04-25 15:48:07
【问题描述】:

如果我的方法完全错误,请说出来。

我正在使用 ASP.Net Identity 2.0。我已经自定义了类(在我的 MembershipDbContext 中),特别是我有 2 个类:

 1. CustomRoles (in fluent api renamed table to "App_CustomRoles")
 2. CustomUserRoles (in fluent api renamed table to "App_CustomUserRoles")

我还有另一个上下文类 ApplicationDbContext,在本次讨论中它与应用程序的菜单系统有关。各个菜单项与 CustomRoles 有关系。也就是说,只有特定角色类型的用户才能看到他们:

public class DbMenu
{
    // Backing Fields
    ...
    private ICollection<DbMenuRole> _dbMenuRoles;

    public DbMenu()
    {
     ...
        _dbMenuRoles = new Collection<DbMenuRole>();
    }
     ...
    public virtual ICollection<DbMenuRole> DbMenuRoles
    {
        get { return _dbMenuRoles; }
        set { _dbMenuRoles = value; }
    }
    public class DbMenuRole
{
    ...
    // Foreign Keys
    [Required]
    public long FK_RoleId { get; set; }
    ...
    // Associations
    [ForeignKey("FK_RoleId")]
    public virtual CustomRole CustomRole { get; set; }
   ...
}

当我运行迁移然后为每个上下文更新数据库时,我的 sql db 有 4 个表,其中 2 个是重复的:

正确的(来自 MembershipDbContext):

 1. App_CustomRoles
 2. App_CustomUserRoles

不正确的(来自 ApplicationDbContext):

 1. CustomRoles
 2. CustomUserRoles

我希望将会员资格和应用程序的导航系统保留在单独的上下文类中。我尝试在应用程序上下文中使用 fluent API 将不正确的表设置为恰当命名的正确表,但这会导致构建错误。

如何在将课程保存在单独的上下文文件中的同时更正此问题?

谢谢。

【问题讨论】:

    标签: entity-framework asp.net-identity-2


    【解决方案1】:

    我不知道这是否是正确的方法,但如果我:

    1. 在 ApplicationDbContext 中禁用自动迁移,然后
    2. 为其添加迁移并注释掉有问题的表,一切正常。

    如果有人能告诉我这是否是正确的方法,我将不胜感激:

    namespace Data.Migrations_ApplicationDbContext
    {
    using System;
    using System.Data.Entity.Migrations;
    
    public partial class ApplicationDbContext : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "dbo.App_DbMenuRoles",
                c => new
                    {
                        Id = c.Long(nullable: false, identity: true),
                        FK_RoleId = c.Long(nullable: false),
                        FK_DbMenuId = c.Long(nullable: false),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.App_CustomRoles", t => t.FK_RoleId, cascadeDelete: true)
                .ForeignKey("dbo.App_DbMenus", t => t.FK_DbMenuId, cascadeDelete: true)
                .Index(t => t.FK_RoleId)
                .Index(t => t.FK_DbMenuId);
    
            //CreateTable(
            //    "dbo.App_CustomRoles",
            //    c => new
            //        {
            //            Id = c.Long(nullable: false, identity: true),
            //            Name = c.String(),
            //        })
            //    .PrimaryKey(t => t.Id);
    
            //CreateTable(
            //    "dbo.App_CustomUserRoles",
            //    c => new
            //        {
            //            Id = c.Long(nullable: false, identity: true),
            //            UserId = c.Long(nullable: false),
            //            RoleId = c.Long(nullable: false),
            //            CustomRole_Id = c.Long(),
            //        })
            //    .PrimaryKey(t => t.Id)
            //    .ForeignKey("dbo.App_CustomRoles", t => t.CustomRole_Id)
            //    .Index(t => t.CustomRole_Id);
    
            CreateTable(
                "dbo.App_DbMenus",
                c => new
                    {
                        Id = c.Long(nullable: false, identity: true),
                        Title = c.String(nullable: false),
                        PrimaryUrl = c.String(nullable: false),
                        SecondaryUrl = c.String(),
                        TertiaryUrl = c.String(),
                        FK_ParentId = c.Long(),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.App_DbMenus", t => t.FK_ParentId)
                .Index(t => t.FK_ParentId);
    
        }
    
        public override void Down()
        {
            DropForeignKey("dbo.App_DbMenus", "FK_ParentId", "dbo.App_DbMenus");
            DropForeignKey("dbo.App_DbMenuRoles", "FK_DbMenuId", "dbo.App_DbMenus");
            DropForeignKey("dbo.App_DbMenuRoles", "FK_RoleId", "dbo.App_CustomRoles");
            DropForeignKey("dbo.App_CustomUserRoles", "CustomRole_Id", "dbo.App_CustomRoles");
            DropIndex("dbo.App_DbMenus", new[] { "FK_ParentId" });
            DropIndex("dbo.App_CustomUserRoles", new[] { "CustomRole_Id" });
            DropIndex("dbo.App_DbMenuRoles", new[] { "FK_DbMenuId" });
            DropIndex("dbo.App_DbMenuRoles", new[] { "FK_RoleId" });
            DropTable("dbo.App_DbMenus");
            DropTable("dbo.App_CustomUserRoles");
            DropTable("dbo.App_CustomRoles");
            DropTable("dbo.App_DbMenuRoles");
            }
        }
    }
    

    【讨论】:

    猜你喜欢
    • 2017-05-16
    • 2015-02-16
    • 2018-03-20
    • 2013-09-11
    • 2014-06-06
    • 2015-05-07
    • 2014-05-04
    • 2018-06-08
    • 2017-06-17
    相关资源
    最近更新 更多