私は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; }
...
}
}