【发布时间】: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.DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.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