Fluent Nhibernate 将多个类映射到单个外键
我有一个数据库结构,其中一个表扩展另一个表,然后第三个表具有在前两个表中有效的外键。
例如:
TABLE Person
PersonId int
Name varchar
ShirtId int
TABLE Shirt
ShirtId int
Color string
TABLE SpecialShirt
ShirtId int
Sleeves int
类如下所示:
public class Person
{
public virtual int PersonId { get; set; }
public virtual string Name { get; set; }
public virtual Shirt Shirt { get; set; }
public virtual SpecialShirt SpecialShirt { get; set; }
}
public class Shirt
{
public Shirt()
{
PersonList = new List<Person>();
}
public virtual int ShirtId { get; set; }
public virtual string Color { get; set; }
public virtual IList<Person> PersonList { get; set; }
public virtual void AddPerson(Person p)
{
PersonList.Add(p);
}
}
public class SpecialShirt : Shirt
{
public virtual int Sleeves { get; set; }
}
映射如下所示:
public class TestPersonMap : ClassMap<TestPerson>
{
public TestPersonMap()
{
Table("TestPerson");
Id(x => x.PersonId).GeneratedBy.Native();
Map(x => x.Name);
References(x => x.Shirt).Column("ShirtId");
References(x => x.SpecialShirt).Column("ShirtId"); // commenting out this line works
}
}
public class TestShirtMap : ClassMap<TestShirt>
{
public TestShirtMap()
{
Table("TestShirt");
Id(x => x.ShirtId).GeneratedBy.Native();
Map(x => x.Color);
HasMany(x => x.PersonList).KeyColumn("ShirtId").Inverse().Cascade.All();
}
}
public class TestSpecialShirtMap : SubclassMap<TestSpecialShirt>
{
public TestSpecialShirtMap()
{
Table("TestSpecialShirt");
KeyColumn("ShirtId");
Map(x => x.Sleeves).Column("Sleeves");
}
}
然后,我尝试保存 Person 和 Shirt 的新实例,如下所示:
var shirt1 = new TestShirt() {Color = "Red"};
var person1 = new TestPerson() {Name = "Fred Person", Shirt = shirt1};
shirt1.AddPerson(person1);
session.Save(shirt1);
session.Save(person1);
此代码出现以下错误:
System.ArgumentOutOfRangeException : Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
如果我删除从 Person 到 SpecialShirt 的引用,它会起作用。我可以在同一列(“ShirtId”)上有两个引用吗?或者有更好的方法吗?我无法真正更改表结构。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这种关系如何运作?
通常,
Person
穿着Shirt
,而Shirt
可以是Shirt
或SpecialShirt< /代码>。因为
SpecialShirt
是Shirt
,所以 Person 只需要拥有对Shirt
的引用。 NHibernate 了解所有关于每个具体类的表映射(这就是您所拥有的)。How does the relationship work?
Usually, a
Person
wears aShirt
, and aShirt
is either aShirt
or aSpecialShirt
. Because aSpecialShirt
is aShirt
, a Person need only have a reference to aShirt
. NHibernate knows all about table-per-concrete-class mappings (which is what you have).我找到了解决方案。它很丑陋,但它有效。我将命令 DynamicInsert() 添加到 PersonMap 中。所以现在的地图是:
然后我必须分别保存衬衫和人,因此级联实际上不起作用。
到目前为止,这是有效的。老实说,我不知道 DynamicInsert 是做什么的。在研究这种方法时,我只能找到过时的文档和无用的论坛帖子。
我只能假设这在未来的某个时候会失败。
I found a solution. It's ugly, but it's working. I added the command DynamicInsert() into the PersonMap. So the map is now:
I then have to save the Shirt and Person separately, so the cascade is not really working.
This is working so far. I honestly don't know what DynamicInsert does. I can only find out-of-date documentation and unhelpful forum threads when researching this method.
I can only assume this will be fail at some point in the future.