是否可以在没有 HasMany 的情况下将外键设置为 null?
我有实体 Territory
class Territory
{
public int Id {get;set;}
public string Title {get;set;}
}
和一些引用它的实体,例如
class MyObject
{
public Territory Territory {get;set;}
}
FluentNHibernate 中的映射
References(x => x.Territory)
.Column("TerritoryId")
.Nullable()
.Not.LazyLoad()
.Cascade.None();
,当我删除领土时,出现错误: DELETE 语句与 REFERENCE 约束“FK377ABC4DAD038F1B”冲突。冲突发生在数据库“GPM_Test”、表“dbo.MyObject”、列“TerritoryId”中。
在删除区域之前,如何让 nhibernate 将 FK 设置为 NULL?
我需要类似 SQL 的东西
ON DELETE set NULL
I have entity Territory
class Territory
{
public int Id {get;set;}
public string Title {get;set;}
}
and some entities which have references to it, such as
class MyObject
{
public Territory Territory {get;set;}
}
and mapping in FluentNHibernate
References(x => x.Territory)
.Column("TerritoryId")
.Nullable()
.Not.LazyLoad()
.Cascade.None();
and when I'm deleting territory I have error: The DELETE statement conflicted with the REFERENCE constraint "FK377ABC4DAD038F1B". The conflict occurred in database "GPM_Test", table "dbo.MyObject", column 'TerritoryId'.
How can I make nhibernate set FK to NULL before territory is deleted?
I need something like in SQL
ON DELETE set NULL
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想说的是,在删除对象本身之前,您必须先删除实体之间的链接,这当然可以在数据库中通过级联删除来完成。另一种方法是以编程方式删除要删除的实体与相关表之间的链接。
I would say you have to delete the links between the entities first before you can delete the object itself, this can ofcourse be done in the database by cascade deletion. Another way is to programmicaly removed the links between the entity you want to remove and the related tables.