Fluent NHibernate 有很多子表没有主键的映射
我已经使用 NHibernate 几周了,所以请耐心等待。我正在开发一个存在很多奇怪问题的遗留数据库。
我有一个名称对象
public class Name {
public Name()
{
Addresses = new List<Address>();
}
public virtual string Id { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual IList<NameAddress> Addresses { get; set; }
}
它有地址的子对象
public class Address
{
public virtual string NameId { get; set; }
public virtual string Line1 { get; set; }
public virtual string City { get; set; }
public virtual string State { get; set; }
public virtual string Zipcode { get; set; }
public virtual string Type { get; set; }
public virtual bool IsPreferrred { get; set; }
}
这是映射 Address 没有定义主键,但 name_id 和 type 使其唯一。您看到的列是表结构。
Table("ADDRESS");
CompositeId()
.KeyProperty(x => x.NameId, "NAME_ID")
.KeyProperty(x => x.Type, "ADDRESS_TYPE");
Map(x => x.IsPreferred)
.Column("PREF");
Map(x => x.Line1)
.Column("ADDRESS1");
Map(x => x.Line2)
.Column("ADDRESS2");
Map(x => x.City)
.Column("CITY");
Map(x => x.State)
.Column("STATE");
姓名 表(“名称”);
Id(x => x.Id)
.GeneratedBy.Custom<XXIdentifierGenerator>(p => p.AddParam("prefix", "NAME"))
.Column("NAME_ID");
Map(x => x.Prefix)
.Column("NAME_PRE");
Map(x => x.FirstName)
.Column("NAME_FIRST");
HasMany(x => x.Addresses)
.KeyColumn("NAME_ID")
.Table("ADDRESS")
.LazyLoad();
我可以创建一个没有任何地址的名称并获取生成的 id。
repository.Save(name); // only calls session.Save and does a commit
Address address = new NameAddress {IsPreferred = true, Type= "Home", Line1 = "123 Main St",
City = "Anytown", State = "CT", Zipcode="06512" };
name.Addresses.Add(address);
repository.SaveOrUpdate(name);
当我尝试保存地址时,出现异常
{"Unexpected row count: 0; expected: 1"}
我不确定
- 我的映射是否错误
- 我不知道如何连接有很多
- 我无法在没有主键的情况下执行此操作
- 在这种情况下地址是否必须自己能得救吗?
谢谢, 保罗
So I'm a couple of weeks into NHibernate so pleae bear with me. I am working on a legacy database with lots of weird issues.
I have a name object
public class Name {
public Name()
{
Addresses = new List<Address>();
}
public virtual string Id { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual IList<NameAddress> Addresses { get; set; }
}
It has children of address
public class Address
{
public virtual string NameId { get; set; }
public virtual string Line1 { get; set; }
public virtual string City { get; set; }
public virtual string State { get; set; }
public virtual string Zipcode { get; set; }
public virtual string Type { get; set; }
public virtual bool IsPreferrred { get; set; }
}
Here are the mappings
Address has no primary key defined but it is name_id and type that make it unique. The columns you see are the table structure.
Table("ADDRESS");
CompositeId()
.KeyProperty(x => x.NameId, "NAME_ID")
.KeyProperty(x => x.Type, "ADDRESS_TYPE");
Map(x => x.IsPreferred)
.Column("PREF");
Map(x => x.Line1)
.Column("ADDRESS1");
Map(x => x.Line2)
.Column("ADDRESS2");
Map(x => x.City)
.Column("CITY");
Map(x => x.State)
.Column("STATE");
Name
Table("NAME");
Id(x => x.Id)
.GeneratedBy.Custom<XXIdentifierGenerator>(p => p.AddParam("prefix", "NAME"))
.Column("NAME_ID");
Map(x => x.Prefix)
.Column("NAME_PRE");
Map(x => x.FirstName)
.Column("NAME_FIRST");
HasMany(x => x.Addresses)
.KeyColumn("NAME_ID")
.Table("ADDRESS")
.LazyLoad();
I can create a name without any addresss and get the generated id back.
repository.Save(name); // only calls session.Save and does a commit
Address address = new NameAddress {IsPreferred = true, Type= "Home", Line1 = "123 Main St",
City = "Anytown", State = "CT", Zipcode="06512" };
name.Addresses.Add(address);
repository.SaveOrUpdate(name);
When I try to save the address I get an exception
{"Unexpected row count: 0; expected: 1"}
I am not sure if
- My mapping is wrong
- I don't get how to wire up a has many
- I can't do this without a primary key
- In this case do the addresses have to be saved on their own?
Thanks,
Paul
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这有点盲目,但是如何在
HasMany(x => x.Addresses)
映射上添加.Cascade.All
或类似的代码>名称?
我想 NHibernate 需要知道您想要将新地址与新名称一起插入。
This is a bit of a shot in the dark, but how about adding
.Cascade.All
or similar on theHasMany(x => x.Addresses)
mapping forName
?I would imagine that NHibernate needs to know that you want new Addresses to be inserted along with a new Name.