Suppliers
をEntity Framework 6.1で取得しようとすると、次のエラーが発生します。EF:プロパティ 'Street'は 'Address'タイプの宣言されたプロパティではありません
プロパティ 'Street'は 'Address'タイプの宣言されたプロパティではありません。 IgnoreメソッドまたはNotMappedAttributeデータアノテーションを使用して、プロパティが モデルから明示的に除外されていないことを確認します。それが有効なプリミティブプロパティであることを確認してください。
SupplierMap:私はまた、定義された2つのマッピングを持って
public class Address : ValueObject<Address> { // public int Id: defined in ValueObject class public string Street { get; } public string Number { get; } public string Zipcode { get; } protected Address() { } protected override bool EqualsCore(Address other) { // removed for sake of simplicity } protected override int GetHashCodeCore() { // removed for sake of simplicity } }
:
Supplier.cs:
public class Supplier : AggregateRoot { // public int Id: defined in AggregateRoot class public string CompanyName { get; private set; } public ICollection<Address> Addresses { get; private set; } protected Supplier() { } public Supplier(string companyName) { CompanyName = companyName; Addresses = new List<Address>(); } public void ChangeCompanyName(string newCompanyName) { CompanyName = newCompanyName; } }
Address.cs
私のエンティティは次のようになり.cs
public class SupplierMap : EntityTypeConfiguration<Supplier>
{
public SupplierMap()
{
// Primary Key
this.HasKey(t => t.Id);
// Properties
this.Property(t => t.CompanyName).IsRequired();
}
}
AddressMap.cs
public class AddressMap : EntityTypeConfiguration<Address>
{
public AddressMap()
{
// Primary Key
this.HasKey(t => t.Id);
// Properties
this.Property(t => t.Street).IsRequired().HasMaxLength(50);
this.Property(t => t.Number).IsRequired();
this.Property(t => t.Zipcode).IsOptional();
}
}
しかし、私は、次のコードを実行したとき、私は私に、上記のエラーを与える:
using (var ctx = new DDDv1Context())
{
var aaa = ctx.Suppliers.First(); // Error here...
}
私はSupplier.cs
クラスからICollection
を削除する際にコードが動作しますまた、dbコンテキストからマッピングクラスを削除します。
public class DDDv1Context : DbContext
{
static DDDv1Context()
{
Database.SetInitializer<DDDv1Context>(null);
}
public DbSet<Supplier> Suppliers { get; set; }
public DbSet<Address> Addresses { get; set; } //Can leave this without problems
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Configurations.Add(new SupplierMap());
// Now code works when trying to get supplier
//modelBuilder.Configurations.Add(new AddressMap());
}
}
Address
クラスとAddresMap
クラスを使用すると、コードでエラーが発生するのはなぜですか?
ありがとう、それは問題でした。しかし、 'Address'クラスは' ValueObject'であり、不変でなければなりません。この問題を解決するために、パブリックなものではなく、それぞれのプロパティに 'protected set'を追加しました。それはトリックを行うようだ。 – w00