最初にコードを使用して、 ID をクラスにYogaSpace
リンクする外部キーを作成しています。そのため、新しいものを作成して挿入するたびに、ID にログインしている人の ID が入力されます。そして、1 つのIDに対してオブジェクトが複数になる可能性がある 1 対多にしたいと考えています。しかし、重要なエラーが発生しています。ApplicationUsers
ApplicationUserRefId
YogaSpace
YogaSpace
ApplicationUser
YogaSpace
ApplicationUser
public class YogaSpace
{
// for a one-to-one relationship
// great tutorial on code-first
//http://www.entityframeworktutorial.net/code-first/configure-one-to-one-relationship-in-code-first.aspx
[Index (IsUnique = true)]
[Key]
[Column(Order = 1)]
public int YogaSpaceId { get; set; }
[Key]
[Column(Order = 2)]
public int ApplicationUserRefId { get; set; }
[ForeignKey("ApplicationUserRefId")]
public virtual ApplicationUser ApplicationUser { get; set; }
[Required]
public string Name { get; set; }
}
ここに私のApplicationUser
クラスがあります
public class ApplicationUser : IdentityUser
{
//other members left out to save space
public virtual ICollection<YogaSpace> YogaSpaces { 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;
}
}
ここにすべてのエラーがあります。データベースを更新すると、キーに関連するスローが発生します。
モデルの生成中に 1 つ以上の検証エラーが検出されました:
GiftExchange.DataLayer.Models.IdentityUserLogin: : EntityType 'IdentityUserLogin' > キーが定義されていません。この EntityType のキーを定義します。GiftExchange.DataLayer.Models.IdentityUserRole: : EntityType 'IdentityUserRole' > キーが定義されていません。この EntityType のキーを定義します。
アップデート!キーの作成を要求するクラスをオーバーライドしようとしましたが、それでも機能しません
public class TestIdentityUserLogin : IdentityUserLogin
{
[Key]
public override string UserId { get; set; }
}
このリンクにある流暢なAPIを使用してキーを追加しようとしました[エンティティタイプMVC5 EF6のユーザー
私YogaSpaceContext
が含めた
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });
}
データベースを更新するためのパッケージマネージャーからキーが定義されていないエラーを削除したようです。しかし、最初にコードにどのように実装しますか?