【问题标题】:Why is code first migrations changing table name?为什么代码优先迁移会更改表名?
【发布时间】:2017-02-16 11:02:24
【问题描述】:

我正在尝试建立外键关系(一个 ---to----many)

并且让 applicationUser 拥有一个现有 person 表的外键。

我的代码。

public class ApplicationUser : IdentityUser
{

    [Required]
    public long PersonId { get; set; }

    //[Required]
    [ForeignKey("PersonId")]
    public virtual Person Person { get; set; }

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }
}

人员表(数据库中存在)

public class Person
{
    [System.ComponentModel.DataAnnotations.KeyAttribute()]
    [System.ComponentModel.DataAnnotations.Schema.DatabaseGe‌​nerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGen‌​eratedOption.None)]
    public long PersonId { get; set; }

    public virtual ICollection<ApplicationUser> Users { get; set; }
}

问题

使用迁移时,我得到以下信息。

向上方法:

        CreateTable(
            "dbo.People",
            c => new
                {
                    PersonId = c.Long(nullable: false),
                })
            .PrimaryKey(t => t.PersonId);

降法:

    public override void Down()
    {
        DropForeignKey("dbo.AspNetUsers", "PersonId", "dbo.People");
        DropIndex("dbo.AspNetUsers", new[] { "PersonId" });

        DropTable("dbo.People");

    }

dbo.People 是从哪里来的???假设去 db.Person 这是一个已经创建的表(通过 sql)。

我该如何解决这个问题?

【问题讨论】:

    标签: asp.net-mvc entity-framework asp.net-mvc-5 ef-code-first


    【解决方案1】:

    默认情况下,Entity Framework 将实体名称复数来命名 DB 表。您可以通过添加以下代码在OnModelCreating 方法中关闭此行为:

    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    

    【讨论】:

    • 谢谢@Alexey Andrushkevich
    猜你喜欢
    • 2016-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多