I have this model:
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 void Address Address {get; set;}
}
The current mapping for the Customer
map is:
this.Property(t => t.AddressId).HasColumnName("address_id");
this.HasOptional(t => t.Address)
.WithMany()
.HasForeignKey(d => d.AddressId);
There is no mapping back to the Customer
in the Address
map.
But this is not working because if I remove the address from the customer, the address record is not deleted from the db.
What is the correct way to add a 1..0-1 relationship where the 0-1 side is owned by the 1 side?
Try this one
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Customer>()
.HasOptional(customer => customer.Address)
.WithRequired()
.Map(mapping => mapping.MapKey("custom_fk_customer_id"));
base.OnModelCreating(modelBuilder);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.