私はこれらのモデルを持っています:
public class Address
{
public int Id {get; set;}
public string Street {get; set;}
public string HouseNo {get; set;}
}
public class Customer
{
public int Id {get; set;}
public string Name {get; set;}
public int AddressId {get; set;}
public virtual Address Address {get; set;}
}
そしてマッピングクラス:
public class AddressMap : EntityTypeConfiguration<Address>
{
public AddressMap()
{
this.ToTable("addresses");
this.HasKey(t => t.Id).Property(t => t.Id).HasColumnName("id")
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
this.Property(t => t.Street).HasColumnName("street");
this.Property(t => t.HouseNumber).HasColumnName("house_no");
}
}
public class CustomerMap : EntityTypeConfiguration<Customer>
{
public CustomerMap()
{
this.ToTable("customers");
this.HasKey(t => t.Id).Property(t => t.Id).HasColumnName("id")
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
this.Property(t => t.Name).HasColumnName("name");
this.Property(t => t.AddressId).HasColumnName("id_address");
this.HasOptional(t => t.Address)
.WithMany()
.HasForeignKey(t => t.AddressId);
}
}
CRUD操作にGraphDiffを使用しています。
住所を使用して新しい顧客を追加すると、正常に機能します。ただし、次のようにして顧客の住所を削除したい場合:
if (customer.Address != null)
{
if (customer.Address.IsEmpty())
{
if (customer.Address.Id > 0)
addressRepository.Delete(customer.Address);
customer.Address = null;
}
}
customerRepository.Update(customer);
そしてリポジトリで:
public override Customer Update(Customer entity)
{
return ((DbContext)dataContext).UpdateGraph<Customer>
(entity,
map => map.OwnedEntity(x => x.Address)
);
}
顧客の住所はデータベースで NULL に設定されています。ただし、アドレスはデータベースから削除されていません。
マッピング クラスの構成が間違っているのでしょうか、それとも GraphDiff が期待どおりに動作しないのでしょうか?