1

我在 asp.net mvc 程序中使用代码优先迁移。

我正在使用项目中提供的默认身份验证和角色。

现在,当我启用迁移时,它会自动生成一个迁移类,该类会生成所有表等。

这是我希望编辑的特定表的示例。

 CreateTable(
            "dbo.AspNetUserRoles",
            c => new
                {
                    UserId = c.String(nullable: false, maxLength: 128),
                    RoleId = c.String(nullable: false, maxLength: 128),
                })
            .PrimaryKey(t => new { t.UserId, t.RoleId })
            .ForeignKey("dbo.AspNetRoles", t => t.RoleId, cascadeDelete: true)
            .ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: true)
            .Index(t => t.RoleId)
            .Index(t => t.UserId);

现在我想在这个表中添加一个描述字段。如果我只是将它添加到数据库中,那将非常容易,但是我将首先丢失我的代码迁移。

1> Entity 框架从哪里获取初始迁移的所有命令?因为我的项目中没有模型可以看到它指定了它创建的表。

2> 如何修改或编辑生成的一些原始表格?我试过只编辑初始迁移文件夹,但这不起作用?

(只是我的想法)难道不是角色和用户模型存储在框架中,而这就是它从中获取表结构的地方吗?如果是这样,我可以不扩展默认模型以添加更多属性吗?因为我知道你可以为 ApplicationUser 做,我以前做过,这里是一个例子:

 // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
public class ApplicationUser : IdentityUser
{
    [Required]
    [MaxLength(50)]
    [Display(Name = "Email Address")]
    public string email { get; set; }
 }

这就是我如何将电子邮件地址字段添加到默认用户。我不能也对角色这样做吗?

4

1 回答 1

1

您对 ApplicationUser 进行了完美的修改,然后对其他内容继续这样做。如果要向用户表添加描述。只需将其添加到 ApplicationUser 模型。不要介意外键和虚拟属性。

    public class ApplicationUser : IdentityUser
    {
        [Required]
        public string FirstName { get; set; }
        [Required]
        public string LastName { get; set; }
        public string GroupName { get; set; }
        [Required]
        public string Email { get; set; }
        [Required]
        [StringLength(15)]
        public string Phone { get; set; }
        public string Remark { get; set; }
        public DateTime? BirthDate { get; set; }
        public DateTime ValidFrom { get; set; }
        public DateTime ValidUntil { get; set; }

        // Foreign keys
        [ForeignKey("Bank")]
        public string AccountNumber { get; set; }
        [ForeignKey("Address")]
        public int? AddressId { get; set; }

        public virtual Bank Bank { get; set; }
        public virtual Address Address { get; set; }
        public virtual ICollection<Request> Requests { get; set; } 
    }

要编辑角色类,您应该在您的类上继承 IdentityRole 并添加属性:

public class ApplicationRole : IdentityRole
{
    public string Description { get; set; }
}

该框架将生成新的迁移类,这些迁移类将在您使用 Update-Database 命令时运行。

您必须更改您的 identityManager(在此处使用 ApplicationRole):

public class IdentityManager
{

    public bool RoleExists(string name)
    {
        var rm = new RoleManager<ApplicationRole>(new RoleStore<ApplicationRole>(new ApplicationDbContext()));

        return rm.RoleExists(name);
    }

    public bool CreateRole(string name)
    {
        var rm = new RoleManager<ApplicationRole>(new RoleStore<ApplicationRole>(new ApplicationDbContext()));

        var idResult = rm.Create(new ApplicationRole(name));

        return idResult.Succeeded;
    }
}

您必须覆盖 ApplicationDbContext 中的角色,如下所示(不要忘记新的):

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

    ...
    public DbSet<Product> Products { get; set; }
    new public DbSet<ApplicationRole> Roles { get; set; }
}
于 2014-01-30T06:43:14.797 回答