0

最初にコードを使用して、 ID をクラスにYogaSpaceリンクする外部キーを作成しています。そのため、新しいものを作成して挿入するたびに、ID にログインしている人の ID が入力されます。そして、1 つのIDに対してオブジェクトが複数になる可能性がある 1 対多にしたいと考えています。しかし、重要なエラーが発生しています。ApplicationUsersApplicationUserRefIdYogaSpaceYogaSpaceApplicationUserYogaSpaceApplicationUser

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 });
    }

データベースを更新するためのパッケージマネージャーからキーが定義されていないエラーを削除したようです。しかし、最初にコードにどのように実装しますか?

4

2 に答える 2

0

私なら次のようにします:クラスに属性をApplicationUser追加し、ForeignKey

public ApplicationUser : IdentityUser {
    [ForeignKey("UserID")]
    public virtual ICollection<YogaSpace> YogaSpaces { get; set; } 

    // other properties
}

そして、それが属しYogaSpaceている追跡したいモデルで、ApplicationUser

public class YogaSpace{
    public string UserId { get; set; }

    // other properties
}

ApplicationUserインスタンス全体をYogaSpaceクラスに格納する必要はなく、UserID自動的に生成されます。stringの が であるように、 が のタイプでIDあることが重要ですApplicationUser

( Foreign Key To Microsoft.AspNet.Identity.EntityFramework.IdentityUser?に対する私の回答を複製しました)

于 2015-03-12T12:52:21.847 に答える
-2

実際、コードファーストを使用している場合は、別のことを試す必要があると思います。たとえば..

あなたの ApplicationUser クラス:

class ApplicationUser
{
     public int Id { get; set; }
     public ICollection<YogaSpace> YogaSpaces { get; set; }
}

YogaSpaces のコレクションがあり、YogaSpace クラスは次のようになります。

public class YogaSpace
{
   public int Id { get; set; };
   public int AnotherProperty { get; set; }
}

そして、EF が適切なテーブルを作成します。

PS:テストされていませんが、それが方法です。

于 2015-01-29T03:04:23.833 に答える