ASP.Net Identity を使用した新しい ASP.NET MVC 5.1 プロジェクトがあります。信頼性が高く、期待できそうですが、今日は SQL を使用した簡単な作業に 9 時間近くも費やしています。
問題は、デフォルトの AspNetUsers テーブルへの外部キー参照を持つテーブルを CodeFirst 経由で作成できないことです。
たとえば、次のようなテーブルを作成しました。住所
public class Address
{
[Key]
public string UserId { get; set; }
public string MyAddress { get; set; }
}
しかし、外部キー参照を作成するにはどうすればいいでしょうか?AspNetユーザー?
上記のプロパティを次のように置き換えてみます
public IdentityUser MyAddress { get; set; }
// Or
public virtual IdentityUser MyAddress { get; set; }
// Or
public virtual ApplicationUser MyAddress { get; set; }
しかし、すべてまだエラーが表示されます:
One or more validation errors were detected during model generation:
MiaPos.Models.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType.
MiaPos.Models.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType.
IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no keys defined.
IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined.
私はまた、モデル作成時これから役職
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });
}
でも走ると追加移行、IdentityUserLogin、IdentityRole、IdentityUserRole テーブルが作成され、プレフィックスが異なるだけで複製されます。(AspNet<->Identity)
最終的にSQLで10秒で実行しましたが、CodeFirstでなぜできないのか知りたいだけです。そして、なぜそのようなことをするのがこんなに難しいのかマイクロソフト 2014 新技術。
ベストアンサー1
ありがとうございますガボール・プレシュ上記のコメントについて。
解決策を見つけました。
ApplicationUserクラスは継承するアイデンティティユーザー(AspNetUsers テーブルを作成する) は、子クラス (子テーブル) の ICollection プロパティを作成する必要があります。
例えば
public virtual ICollection<ToDo> ToDoes { get; set; }
だからやることクラスは参照できるアプリケーションユーザー
ぜひサンプルをご覧になることをお勧めしますガボール・プレシュ言った。