0

我有这些模型:

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

我正在使用 GraphDiff 进行 CRUD 操作。

添加具有地址的新客户可以正常工作。但是,当我想通过执行以下操作删除客户的地址时:

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 的行为不符合预期?

4

0 回答 0