[英]Nhibernate class mapping and cascade property
We have an Address
entity, which has a foreign key that references a City
table. 我们有一个
Address
实体,它具有引用City
表的外键。 An Address
only references one City
, but a City
should be able to be referenced by many Address
es. 一个
Address
仅引用一个City
,但是一个City
应该能够被多个Address
引用。
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. 这是
Address
类的映射。
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: 我应该如何更改类映射,以便:
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. 到目前为止,我一直在玩
Cascade
属性,并且遇到过这样的问题,其中地址的城市更新导致City表的更新,或者我不能在不违反外键约束的情况下删除地址。
The many-to-one
part, shown above (and discussed also here NHibernate Many-to-one cascade ) could be set to these values 可以将上面显示的
many-to-one
一部分(也将在此处讨论NHibernate多对一级联 )设置为这些值
// xml
cascade="all|none|save-update|delete"
// maping by code
Cascade.All | Cascade.None | Cascade.Persist | Cascade.Remove
// fluent
Cascade.All() | Cascade.SaveUpdate() | Cascade.None() | Cascade.Delete()
(read more here Mapping-by-Code - ManyToOne ) (在此处了解更多信息,按代码映射-ManyToOne )
This will cover point number... well none of the above. 这将涵盖点号...以上都不是。 Becuase none of the requirements is asking for cascading from Address to city.
因为没有任何要求要求从地址到城市的级联。
The point 3: 要点3:
- I can update an Address's City property by updating the foreign key?
我可以通过更新外键来更新地址的城市属性吗?
is not about cascading. 与级联无关。 This is standard behviour, working even without cascading, because it changes the value in the Address table.
这是标准行为,即使没有级联也可以工作,因为它会更改“地址”表中的值。 Not touching the City at all.
完全不接触城市。
To cover the point 2: 涵盖要点2:
- When I delete an Address, the City is NOT touched?
当我删除地址时,城市没有被触及吗?
Well, we should remove cascading at all, because ... it is not needed in this direction. 好吧,我们应该完全删除级联,因为...在此方向上不需要。 But, this setting will fit to point 2
但是,此设置将适合第2点
this.ManyToOne(
e => e.City,
m =>
{
m.Column("id_city");
m.Cascade(Cascade.Persist);
To cover first point: 涵盖第一点:
- When I delete a City, all the Addresses with that City are deleted?
删除城市时,具有该城市的所有地址都会被删除吗?
we have to do a lot. 我们必须做很多事情。 We need to extend the POCO relations and introduce the one-to-many mapping.
我们需要扩展POCO关系并引入一对多映射。 That will do what needed:
那将完成所需的工作:
public class City : Entity
{
public virtual IList<Address> Addresses { get; set; }
}
mapping 制图
mapping by code: 通过代码映射:
Set(x => x.Addresses, c =>
{
...
c.Cascade(Cascade.All.Include(Cascade.DeleteOrphans));
fluent version 流利的版本
HasMany(x => x.Addresses)
...
Cascade.AllDeleteOrphan();
(read more here Mapping-by-Code - Set and Bag ) (在此处了解更多信息,按代码映射-套装和袋子 )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.