efd*_*dee 7 .net nhibernate fluent-nhibernate
我目前正在使用NHibernate作为我的数据访问层,使用Fluent NHibernate为我创建映射文件.我有两个类,TripItem和TripItemAttributeValue,它们之间有多对多的关系.
映射如下:
public class TripItemMap : ClassMap<TripItem2>
{
public TripItemMap()
{
WithTable("TripItemsInt");
NotLazyLoaded();
Id(x => x.ID).GeneratedBy.Identity().WithUnsavedValue(0);
Map(x => x.CreateDate, "CreatedOn").CanNotBeNull();
Map(x => x.ModifyDate, "LastModified").CanNotBeNull();
/* snip */
HasManyToMany<TripItemAttributeValue>(x => x.Attributes).AsBag()
.WithTableName("TripItems_TripItemAttributeValues_Link")
.WithParentKeyColumn("TripItemId")
.WithChildKeyColumn("TripItemAttributeValueId")
.LazyLoad();
}
}
public class TripItemAttributeValueMap : ClassMap<TripItemAttributeValue>
{
public TripItemAttributeValueMap()
{
WithTable("TripItemAttributeValues");
Id(x => x.Id).GeneratedBy.Identity();
Map(x => x.Name).CanNotBeNull();
HasManyToMany<TripItem2>(x => x.TripItems).AsBag()
.WithTableName("TripItems_TripItemAttributeValues_Link")
.WithParentKeyColumn("TripItemAttributeValueId")
.WithChildKeyColumn("TripItemId")
.LazyLoad();
}
}
Run Code Online (Sandbox Code Playgroud)
在我的应用程序中的某个时刻,我从数据库中获取现有属性,将它们添加到tripItem.Attributes,然后保存tripItem对象.最后,TripItems_TripItemAttributeValues_Link永远不会获得任何新记录,从而导致关系不被持久化.
如果有帮助,这些是Fluent NHibernate为这些类生成的映射文件:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="true" assembly="ETP.Core" namespace="ETP.Core.Domain">
<class name="TripItem2" table="TripItemsInt" xmlns="urn:nhibernate-mapping-2.2" lazy="false">
<id name="ID" column="ID" type="Int32" unsaved-value="0">
<generator class="identity" />
</id>
<property name="CreateDate" column="CreatedOn" type="DateTime" not-null="true">
<column name="CreatedOn" />
</property>
<property name="ModifyDate" column="LastModified" type="DateTime" not-null="true">
<column name="LastModified" />
</property>
<bag name="Attributes" lazy="true" table="TripItems_TripItemAttributeValues_Link">
<key column="TripItemId" />
<many-to-many column="TripItemAttributeValueId" class="ETP.Core.Domain.TripItemAttributeValue, ETP.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</bag>
</class>
</hibernate-mapping>
Run Code Online (Sandbox Code Playgroud)
和
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="true" assembly="ETP.Core" namespace="ETP.Core.Domain">
<class name="TripItemAttributeValue" table="TripItemAttributeValues" xmlns="urn:nhibernate-mapping-2.2">
<id name="Id" column="Id" type="Int32">
<generator class="identity" />
</id>
<property name="Name" column="Name" length="100" type="String" not-null="true">
<column name="Name" />
</property>
<bag name="TripItems" lazy="true" table="TripItems_TripItemAttributeValues_Link">
<key column="TripItemAttributeValueId" />
<many-to-many column="TripItemId" class="ETP.Core.Domain.TripItem2, ETP.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</bag>
</class>
</hibernate-mapping>
Run Code Online (Sandbox Code Playgroud)
我在这做错了什么?
@efdee
我遇到了同样的问题,花了将近两天的时间.我有很多关系,链接表也没有更新.我是NHibernate的新手,只是想学习它,所以把我说的一切都拿出来.
好吧,事实证明它不是流畅的NHibernate,也不是映射,但我不理解NHibernate如何与多对多一起工作.在多对多关系中,如果未填充两个实体上的集合,则NHibernate不会将数据持久保存到链接表.
假设我将这些实体与多对多关系:
partial class Contact
{
public string ContactName {get; set;}
public IList Locations {get; set;}
}
partial class Location
{
public string LocationName {get; set;}
public string LocationAddress {get;set;}
public IList Contacts {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
当我向Contact.Locations添加一个位置时,我必须确保该联系人也存在于location.Contacts中.
所以要添加一个位置我在我的Contact类中有这个方法.
public void AddLocation(Location location)
{
if (!location.Contacts.Contains(this))
{
location.Contacts.Add(this);
}
Locations.Add(location);
}
Run Code Online (Sandbox Code Playgroud)
这似乎解决了我的问题,但就像我说我只是拿起NHibernate并学习它,可能有更好的方法.如果有人有更好的解决方案,请发布.
这是指向我检查两个集合的帖子:http://www.coderanch.com/t/217138/Object-Relational-Mapping/link-table-of-ManyToMany-annotation
| 归档时间: |
|
| 查看次数: |
7123 次 |
| 最近记录: |