我一直在尝试使用Entity Framework 4.4,NHibernate 3.3.1.4000和SQL Server,我注意到在提交更改时修复关系有所不同,我想知道什么是最佳实践或者是我做错了。
这是我测试的内容。我有一个与n个孩子相关的经典父母。我在数据库中有2个父母,每个父母有20个孩子。我加载了父母双方,我带着第一个父母的第一个孩子,并将该孩子分配给第二个父母。然后我提交更改。
在EF中,在我保存之后,我可以看到父母双方儿童收藏的计数已被改变,因此它修复了关系。
然而,当我在NHibernate中做同样的事情时,计数仍然是相同的。
这是我的代码设置,可以重现这个问题。
波苏斯:
public class Parent
{
public virtual int ParentId { get; set; }
public virtual string Name { get; set; }
public virtual IList<Child> Children { get; set; }
public Parent()
{
Children = new List<Child>();
}
}
public class Child
{
public virtual int ChildId { get; set; }
public virtual int ParentId { get; set; }
public virtual string Name { get; set; }
public virtual Parent Parent { get; set; }
}
NHibernate父映射:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="ConsoleApplication1"
namespace="ConsoleApplication1">
<class name="Parent" table="Parents">
<id name="ParentId" column="ParentId" />
<property name="Name" column="Name" />
<bag name="Children" cascade="all">
<key column="ParentId"/>
<one-to-many class="Child"/>
</bag>
</class>
</hibernate-mapping>
NHibernate子映射:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="ConsoleApplication1"
namespace="ConsoleApplication1">
<class name="Child" table="Children">
<id name="ChildId" column="ChildId" />
<property name="Name" column="Name" />
<many-to-one name="Parent" class="Parent" column="ParentId" not-null="true" />
</class>
</hibernate-mapping>
EF DbContext:
public class Entities : DbContext
{
public DbSet<Parent> Parents { get; set; }
public DbSet<Child> Children { get; set; }
}
App.config中:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler,NHibernate" />
</configSections>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">
NHibernate.Connection.DriverConnectionProvider
</property>
<property name="dialect">
NHibernate.Dialect.MsSql2005Dialect
</property>
<property name="connection.driver_class">
NHibernate.Driver.SqlClientDriver
</property>
<property name="connection.connection_string">
Data Source=localhost;Initial Catalog=MaintainRelationshipsNH;Integrated Security=True;
</property>
</session-factory>
</hibernate-configuration>
<connectionStrings>
<add
name="Entities"
providerName="System.Data.SqlClient"
connectionString="Server=localhost;Database=MaintainRelationshipsNH;Trusted_Connection=true;"/>
</connectionStrings>
</configuration>
创建表格和数据的脚本:
create table Parents (
ParentId INT not null,
Name NVARCHAR(255) null,
primary key (ParentId)
)
create table Children (
ChildId INT not null,
Name NVARCHAR(255) null,
ParentId INT not null,
primary key (ChildId)
)
alter table Children
add constraint FK_Children_Parents
foreign key (ParentId)
references Parents
declare @idChild int
insert into Parents (ParentId, Name) values (0, 'John');
set @idChild = 0
while @idChild < 20
begin
insert into Children (ChildId, Name, ParentId) values (@idChild, 'Child ' + convert(nvarchar(2), @idChild), 0);
set @idChild = @idChild + 1
end
insert into Parents (ParentId, Name) values (1, 'Julie');
while @idChild < 40
begin
insert into Children (ChildId, Name, ParentId) values (@idChild, 'Child ' + convert(nvarchar(2), @idChild), 1);
set @idChild = @idChild + 1
end
NHibernate测试代码:
System.Diagnostics.Debug.WriteLine("Test NHibernate:");
Configuration configuration = new Configuration();
configuration.Configure();
configuration.AddAssembly(typeof(Parent).Assembly);
ISessionFactory sessionFactory = configuration.BuildSessionFactory();
Parent parent0, parent1;
using (ISession session = sessionFactory.OpenSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
parent0 = session.Load<Parent>(0);
parent1 = session.Load<Parent>(1);
System.Diagnostics.Debug.WriteLine("Before modifications and commit");
System.Diagnostics.Debug.WriteLine("Parent0 number of children: " + parent0.Children.Count);
System.Diagnostics.Debug.WriteLine("Parent1 number of children: " + parent1.Children.Count);
parent0.Children[0].Parent = parent1;
transaction.Commit();
}
}
System.Diagnostics.Debug.WriteLine("After modifications and commit");
System.Diagnostics.Debug.WriteLine("Parent0 number of children: " + parent0.Children.Count);
System.Diagnostics.Debug.WriteLine("Parent1 number of children: " + parent1.Children.Count);
实体框架测试代码:
System.Diagnostics.Debug.WriteLine("Test Entity Framework:");
Parent parent0, parent1;
using (Entities entities = new Entities())
{
parent0 = entities.Parents.Find(0);
parent1 = entities.Parents.Find(1);
System.Diagnostics.Debug.WriteLine("Before modifications and commit");
System.Diagnostics.Debug.WriteLine("Parent0 number of children: " + parent0.Children.Count);
System.Diagnostics.Debug.WriteLine("Parent1 number of children: " + parent1.Children.Count);
parent0.Children[0].Parent = parent1;
entities.SaveChanges();
}
System.Diagnostics.Debug.WriteLine("After modifications and commit");
System.Diagnostics.Debug.WriteLine("Parent0 number of children: " + parent0.Children.Count);
System.Diagnostics.Debug.WriteLine("Parent1 number of children: " + parent1.Children.Count);
所以基本上通过这个测试,我可以看到计数随着EF的变化而不是NHibernate。我是在做NH的错误还是我必须手动管理受我的更改影响的关系的每个部分?谢谢!
答案 0 :(得分:3)
在NHibernate中,您需要手动将它们从一个父集合移动到另一个父集合。我通常在父类中使用Add或Remove方法来执行此操作。以下是这些添加或删除方法的示例:
public virtual void AddLine(OrderLine orderLine)
{
orderLine.Order = this;
this.orderLines.Add(orderLine);
}
public virtual void RemoveLine(OrderLine orderLine)
{
this.orderLines.Remove(orderLine);
}
为了重新教育一个孩子,我会做这样的事情:
originalParent.RemoveLine(child);
newParent.AddLine(child);