我需要一對一(可選)。爲什麼EF生成外鍵?
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<PinnacleAccount>().HasKey(x => x.Id);
modelBuilder.Entity<PinnacleAccount>()
.HasRequired(x => x.User)
.WithOptional(x => x.PinnacleAccount);
base.OnModelCreating(modelBuilder);
}
,當我運行「添加 - 遷移初始化」我檢查產生的遷移和看到:
CreateTable(
"dbo.PinnacleAccounts",
c => new
{
Id = c.Int(nullable: false, identity: true),
ClientId = c.String(),
Password = c.String(),
PercentForBet = c.Int(nullable: false),
UserId = c.String(),
User_Id = c.String(nullable: false, maxLength: 128),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.AspNetUsers", t => t.User_Id)
.Index(t => t.User_Id);
但我有屬性用戶ID。爲什麼根據慣例EF創建USER_ID
public class ApplicationUser : IdentityUser
{
public virtual PinnacleAccount PinnacleAccount { get; set; }
public int? PinnacleAccountId { 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;
}
}
'User'實體是怎麼樣的? – JotaBe
請添加所有相關代碼。 – JotaBe