We have an Address
entity, which has a foreign key that references a City
table. An Address
only references one City
, but a City
should be able to be referenced by many Address
es.
public class Address : Entity
{
public virtual string AddressLine1 { get; set; }
public virtual string AddressLine2 { get; set; }
public virtual City City { get; set; }
}
public class City : Entity
{
public virtual string CityName { get; set; }
}
This is the Address
class mapping.
public class AddressClassMapping : ClassMapping<Address>
{
public AddressClassMapping()
{
this.Schema("dbo");
this.Table("addresses");
this.Cache(e => e.Usage(CacheUsage.ReadWrite));
this.Id(e => e.Id,
m => { m.Column("id_address"); m.Generator(Generators.Native);});
this.Property(e => e.AddressLine1,
m => { m.Column("address_line_1"); m.NotNullable(true); });
this.Property(e => e.AddressLine2,
m => { .Column("address_line_2");
m.NotNullable(true); });
this.ManyToOne(
e => e.City,
m =>
{
m.Column("id_city");
m.Cascade(Cascade.All);
});
}
}
How should I change the class mappings so that:
- When I delete a City, all the Addresses with that City are deleted?
- When I delete an Address, the City is NOT touched?
- I can update an Address's City property by updating the foreign key?
So far, I have played with the Cascade
property and have faced issues in which an update to an address's city caused updates to the City table, or I couldn't delete an address without violating a foreign key constraint.