【问题标题】:Add an entity to your initial migration将实体添加到您的初始迁移
【发布时间】:2014-01-30 06:35:29
【问题描述】:

我在 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; }
 }

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

【问题讨论】:

    标签: entity-framework asp.net-mvc-5 asp.net-authentication


    【解决方案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; }
    }
    

    【讨论】:

    • 我想在Roles 表中添加描述。我该怎么做?我要扩展什么课程?
    • 我遇到了问题。我已经按照你的建议做了。但是现在当我访问我的数据库 db.Roles 时,角色的类型是 IntentyRole 而不是 ApllicationRole 我该如何更改?注意
    猜你喜欢
    • 2013-03-17
    • 1970-01-01
    • 1970-01-01
    • 2018-03-07
    • 1970-01-01
    • 2018-11-17
    • 1970-01-01
    • 1970-01-01
    • 2015-04-21
    相关资源
    最近更新 更多