实体框架拆分表中删除 [英] Entity Framework Split Table Delete
问题描述
我使用EF 4 STE的模型一个附件对象。在附件包含了名称,描述,日期,以及最重要的数据(字节[]
)。为了优化加载,我不想要检索的数据属性,直到它是绝对必要的,即当用户点击下载
从客户端。
I'm using EF 4 STE's to model an Attachment object. The Attachment contains a Name, Description, Date, and most importantly Data (byte[]
). To optimize loading, I don't want to retrieve the Data property until it's absolutely necessary, i.e. when the user clicks Download
from the client.
在努力遵循这种方法,我用的说明表格分裂技术的这里。我拆我的附件表成的附件(名称,描述,日期)和 AttachmentData STRONG>(数据)。它在我的EF模型1对1的关系。直到我尝试删除的附件无一切的伟大工程,在 AttachmentData STRONG>(即 attachment.AttachmentData == NULL
)。我得到以下异常:
In an effort to follow this approach, I used the table-splitting technique described here. I split my Attachment table up into Attachment (Name, Description, Date) and AttachmentData (Data). It's a 1-to-1 relationship in my EF model. Everything works great until I try to delete an Attachment without the AttachmentData (i.e. attachment.AttachmentData == null
). I get the following exception:
遇到无效数据。一个必需的关系缺失。检查StateEntries确定违反约束的来源。
Invalid data encountered. A required relationship is missing. Examine StateEntries to determine the source of the constraint violation.
我看到了的类似的帖子,但我似乎无法使其与STE的和工作 。ApplyChanges
扩展方法
I saw a similar post, but I can't seem to make it work with STE's and the ApplyChanges
extension method.
现在我的数据访问代码很简单:我叫上下文的 ApplyChanges()
然后按的SaveChanges()
。
Right now my data access code is simple: I call context's ApplyChanges()
followed by SaveChanges()
.
我已经尝试了简单删除SQL函数,并映射它这两个实体和实际工作;然而,它打破了刀片。我似乎无法映射所有属性两个实体插入功能。
I've tried a simple delete SQL function and mapped it to both entities and that actually worked; however it broke the insert. I can't seem to map an insert function for all properties to both entities.
这是一些其他的选择,我可以尝试任何想法?附件和AttachmentData之间的关系可以选配?当我让1 0+,我得到一个映射错误说它们的主键可能发生碰撞
。
Any ideas on some other options I can try? Can the relationship between Attachment and AttachmentData be optional? When I make it 1 to 0+, I get a mapping error saying that Their primary keys may collide
.
开放给任何建议。
谢谢!
推荐答案
解决方案是类似链接的问题,但你必须使用国有贸易企业的特定功能 - ApplyChanges
:
The solution is similar to linked question but you must use specific feature of STEs - ApplyChanges
:
context.Attachments.ApplyChanges(att);
if (context.ObjectStateManager.GetObjectStateEntry(att).State == EntityState.Deleted)
{
var data = new AttachmentData() {Id = att.Id};
context.AttachmentDataSet.Attach(data);
context.AttachmentDataSet.DeleteObject(data);
}
context.SaveChanges();
这篇关于实体框架拆分表中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!