实体框架拆分表删除 [英] Entity Framework Split Table Delete
问题描述
byte []
)。要优化加载,我绝对不需要检索Data属性,直到用户从客户端点击下载
为止。 为了遵循这种方法,我使用了描述的表分解技术这里。我将附件表分成附件(名称,说明,日期)和 AttachmentData (数据)。在我的EF模型中,这是一对一的关系。一切都很好,直到我尝试删除附件,而不附加 AttachmentData (即 attachment.AttachmentData == null
)。我得到以下异常:
遇到无效数据。缺少必需的关系。检查StateEntries以确定约束违规的来源。
我看到一个类似的帖子,但我似乎无法使其与STE's和 ApplyChanges
扩展方法。
现在我的数据访问代码很简单:我调用上下文的 ApplyChanges()
后跟 SaveChanges()
。
我已经尝试过一个简单的删除SQL函数并将其映射对实体和实际工作;但是它打破了插入。我似乎无法将所有属性的insert函数映射到两个实体。
有些其他选项的想法可以尝试吗? Attachment和AttachmentData之间的关系可以是可选的吗?当我使它1到0+时,我得到一个映射错误,说他们的主键可能会碰撞
。
打开任何建议。
谢谢!
解决方案是类似于链接问题,但您必须使用STE的特定功能 - 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();
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.
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:
Invalid data encountered. A required relationship is missing. Examine StateEntries to determine the source of the constraint violation.
I saw a similar post, but I can't seem to make it work with STE's and the ApplyChanges
extension method.
Right now my data access code is simple: I call context's ApplyChanges()
followed by SaveChanges()
.
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.
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
.
Open to any suggestions.
Thanks!
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屋!