Pregunta

If my approach is wrong altogether please speak out.

I am utilizing ASP.Net Identity 2.0. I have customized the classes (in my MembershipDbContext), in particular I have 2 classes:

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

I have another context class, ApplicationDbContext that in this discussion pertains to the app's menu system. Respective menu items have a relationship with CustomRoles. That is to say, only users of a certain role type will be able to see them:

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; }
   ...
}

When I run migrations and then update database for each context, my sql db has 4 tables of which 2 are duplicates:

Correct Ones (From MembershipDbContext):

 1. App_CustomRoles
 2. App_CustomUserRoles

Incorrect Ones (From ApplicationDbContext):

 1. CustomRoles
 2. CustomUserRoles

I would prefer to keep the Membership and the App's nav system in separate context classes. I tried fluent API in Application context to set the incorrect tables off to the aptly named correct tables, but that caused a build error.

How can I correct this whilst keeping my classes in seperate context files?

Thank You.

¿Fue útil?

Solución

I do not know if this is the correct approach but if I:

  1. Disable automatic migrations in ApplicationDbContext, then
  2. Add a migration for it and comment out the tables in question, all works.

I would appreciate it if someone will tell me if this is the correct approach:

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");
        }
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top