0

私はef5 codefirstでmvc4アプリに取り組んでいますが、このエラーを解決できません:

ID 'xxxx' を持つメンバーは、メタデータ コレクションに存在しません。

更新: 2 つの異なるコンテキストを使用していることがわかりました (ナビゲーション オブジェクトは、別の DbContext を作成するリポジトリを通じて呼び出されました)。これはおそらく問題です。私はそれを変更しましたが、今では新しいエラーが発生します:

列名「Brewery_BreweryId」が無効です。

IntelliTrace で、ef がしようとしていることがわかりました

select ..., Brewery_BreweryId from UserProfiles 

この列は存在せず、存在すべきではありません。1 対多ではなく、多対多が必要です。

それは多対多の関係に関連するものだと思います。

これは私のコードの例です

internal class BreweryConfiguration : EntityTypeConfiguration<Brewery>
{
    public BreweryConfiguration()
    {
      // PK
      HasKey(e => e.BreweryId);

      // FK
      HasMany(e => e.UserProfiles)
        .WithMany()
        .Map(m =>
        {
          m.MapLeftKey("BreweryId");
          m.MapRightKey("UserId");
          m.ToTable("BreweryUserProfiles");
        });

    namespace Project2.DAL.Entities
    {
      [Table("Breweries")]
      public class Brewery : ABrewery
      {
        public int BreweryId { get; set; }
        public ICollection<UserProfile> UserProfiles { get; set; }
      }
    }

    namespace Project1.DAL.Entities
    {
      [Table("UserProfiles")]
      public class UserProfile : IUserProfile
      {

        [Key]
        public int UserId { get; set; }
        ...
      }
}

テーブルに参加

4

1 に答える 1

1
c.MapLeftKey("ClassB_ID");
c.MapRightKey("ClassA_ID");

する必要があります

c.MapLeftKey("ClassA_ID");
c.MapRightKey("ClassB_ID");

編集:

構成で ClassB の PK も定義する必要があります。実装した方法で、ClassB の別の派生構成を追加できます。

于 2013-02-18T18:37:43.567 に答える