我正在使用流畅的api来设置我的实体数据库的关系。我有典型的AspNetUsers表,这个表有一个名为Id(String)的列,它是表的主键。我扩展了IdentityUser并为其提供了其他属性,其中2个属性是AdvisorProfile和StudentProfile。
ApplicationUser具有可选的AdvisorProfile和StudentProfile。 AdvsiorProfile和StudentProfile需要Account(ApplicationUser)。
我的问题是,当表生成AdvisorProfile时,有一个名为Account_Id的属性,它是返回AspNetUser表中ApplicationUser的链接。我想改变命名惯例,但我的流畅api代码似乎并没有改变。
帐户的流利Api - >顾问/帐户外键命名
//Advisor Profile to Login Account
modelBuilder.Entity<ApplicationUser>().HasOptional(x => x.AdvisorProfile).WithRequired(x => x.Account).Map(x => x.MapKey("AdvisorFK"));
modelBuilder.Entity<ApplicationUser>().HasOptional(x => x.AdvisorProfile).WithRequired(x => x.Account).WillCascadeOnDelete(false);
modelBuilder.Entity<Advisor>().HasRequired<ApplicationUser>(x => x.Account).WithOptional(x => x.AdvisorProfile).Map(x => x.MapKey("AccountFK"));
modelBuilder.Entity<Advisor>().HasRequired<ApplicationUser>(x => x.Account).WithOptional(x => x.AdvisorProfile).WillCascadeOnDelete(false);
表生成代码:
CreateTable(
"dbo.Advisors",
c => new
{
Id = c.Long(nullable: false, identity: true),
FirsName = c.String(),
LastName = c.String(),
MiddleName = c.String(),
Email = c.String(),
PhoneNumber = c.String(),
PrimaryContact = c.Int(nullable: false),
Account_Id = c.String(nullable: false, maxLength: 128),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.AspNetUsers", t => t.Account_Id) //Here is the issue
.Index(t => t.Account_Id);
答案 0 :(得分:2)
问题是你对同一个关系使用多个流畅的配置,这很容易出错,很难说哪个实际适用。
根据经验,每个关系始终使用单个流畅的映射。在你的情况下,它应该是这样的:
//Advisor Profile to Login Account
modelBuilder.Entity<ApplicationUser>()
.HasOptional(x => x.AdvisorProfile)
.WithRequired(x => x.Account)
.Map(x => x.MapKey("AdvisorFK"))
.WillCascadeOnDelete(false);