具有 UniqueKey 约束的一对多关系
我是 Fluent-NHibernate 的新手。 我的问题是想要在两个实体之间建立一对多的关系。 一款产品可以有多个(唯一)修订版,并且特定修订版仅属于一个产品。 例如,Product1 有一个修订版“a”和一个修订版“b”,但不能有两个修订版“a”。这就是我的类定义的样子:
public class Product
{
public virtual int ID {get; private set;}
public IList<ProductRevision> revisions { get; set; }
}
public class ProductRevision
{
public virtual int ID {get; private set;}
public Product isRevisionOf { get; set; }
public virtual string revision { get; set; }
}
这是我的映射
public class ProductMap : ClassMap<Product>
{
public ProductMap()
{
Id(x => x.ID).Column("ProductNo");
HasMany(x => x.revisions).Cascade.All();
}
}
public class ProductRevisionMap : ClassMap<ProductRevision>
{
public ProductDefinitionFormationMap()
{
Id(x => x.ID);
References(x => x.isRevisionOf).UniqueKey("Product_Revision").Not.Nullable();
Map(x => x.revision).UniqueKey("Product_Revision").Not.Nullable();
}
}
我现在得到的是 ProductRevision 表中的冗余列“Product_Id”。我用 hasMany() 和references() 定义双方的关系是不是出了什么问题? 通常我不会定义 References(...) 映射,但我需要它作为 uniquekey 约束,不是吗?
谢谢, 埃里克
I'm new to Fluent-NHibernate.
My problem is that a want to establish a one-to-many relationship between two entities.
A product can have multiple (unique) revisions, and a specific revision only belongs to one product.
So for example Product1 has a revision "a" and a revision "b", but can't have two revisions "a". That's what my class definitions look like:
public class Product
{
public virtual int ID {get; private set;}
public IList<ProductRevision> revisions { get; set; }
}
public class ProductRevision
{
public virtual int ID {get; private set;}
public Product isRevisionOf { get; set; }
public virtual string revision { get; set; }
}
Here's my mapping
public class ProductMap : ClassMap<Product>
{
public ProductMap()
{
Id(x => x.ID).Column("ProductNo");
HasMany(x => x.revisions).Cascade.All();
}
}
public class ProductRevisionMap : ClassMap<ProductRevision>
{
public ProductDefinitionFormationMap()
{
Id(x => x.ID);
References(x => x.isRevisionOf).UniqueKey("Product_Revision").Not.Nullable();
Map(x => x.revision).UniqueKey("Product_Revision").Not.Nullable();
}
}
What I get now is redundant column "Product_Id" in the ProductRevision table. A'm I getting something wrong by defining the relation on BOTH sides with hasMany() and references().
Normally I wouldn't define the References(...) mapping, but I need it for the uniquekey constraint, don't I?
Thanks,
Erik
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是双向一对多关系,映射可能如下所示:
Inverse 和 KeyColumn 是这个难题中缺失的部分。
此映射在数据库中生成所需的约束,但您仍然需要在业务逻辑中检查此约束,否则如果将具有重复名称的修订版本插入修订集合中,您将得到 SqlException。
This is bi-directional one-to-many relation and the mapping may look something like this:
Inverse and KeyColumn are the missing pieces of this puzzle.
This mapping generates required constraint in database but you will still need to check this constraint in business logic as well otherwise you will get SqlException if a revision with duplicate name is inserted into revisions collection.