在 Fluent-nhibernate 中,保存多对一实体,为什么我应该为引用的实体提供一个版本
我有以下实体:
public class Worker
{
public int WorkerID {get;set;}
public string Name { get;set;}
public int version { get;set;}
}
public class TransferOrder
{
public int TransferOrderID { get;set;}
public Worker workerTobeTransfered{get;set;}
public int version { get;set;}
}
并且我正在使用流畅的 nhibernate 中的自动映射。 当我尝试像这样保存 TransferOrder 时:
TransferOrder order = new TransferOrder();
order.Worker = new Worker(){WorkerID = 1};
Session.Save(order);
但在数据库中,TransferOrder 表中的workerID 为 NULL??? 但是当我向工作人员提供版本时,它会正常保存吗?
TransferOrder order = new TransferOrder();
order.Worker = new Worker(){WorkerID = 1,Version = 1};
Session.Save(order);
请注意,为工作程序提供的版本号并不重要,只要它不为 0 即可。 我在数据库中保存了一个工人,其工人 ID = 1。
我该如何处理这个问题?为什么我应该给worker一个版本???nhibernate是否确保worker被保存?为什么它应该这样做?
i have the following entities:
public class Worker
{
public int WorkerID {get;set;}
public string Name { get;set;}
public int version { get;set;}
}
public class TransferOrder
{
public int TransferOrderID { get;set;}
public Worker workerTobeTransfered{get;set;}
public int version { get;set;}
}
and i am using the Auto mapping, in fluent nhibernate.
when i try to save the TransferOrder like this:
TransferOrder order = new TransferOrder();
order.Worker = new Worker(){WorkerID = 1};
Session.Save(order);
but in database, the workerID in the TransferOrder table is NULL???
but when i give a version to the worker, it is saved as normal?
TransferOrder order = new TransferOrder();
order.Worker = new Worker(){WorkerID = 1,Version = 1};
Session.Save(order);
notice that it is not important what version number is given to the worker as long as it is not 0.
and i have a worker saved in the database with workerID = 1.
how can i handle this ? why should i give a version to the worker???is the nhibernate making sure that worker is saved?? and why it should do that ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许这是这样的场景:
您的 Work.WorkerID 是工作表中的一个标识列,并且您无法在表中插入仅包含标识列条目的行。
但是,当您也为 Work.Version 提供值时,您就创建了一个有效的插入。
Maybe this is the scenerio:
Your Work.WorkerID is an identity column within your Work table and, you cannot insert a row into your table that consists of only an identity column entry.
But when you provide a value for Work.Version, as well, then you are creating a valid insert.
“版本”可能会自动映射到 NHibernate 属性以进行乐观并发检查。我会使用 Fluent NHibernate 生成映射 XML 以查看它使用的内容,因为我无法通过 Google 找到有关自动映射的默认设置的任何信息。
The "version" is probably going to be auto mapped to a NHibernate attribute for optimistic concurrency checking. I would use Fluent NHibernate to generate the mapping XML to see what it's using, as I can't find anything via Google about the default settings for auto mapped .