层次结构 - 基类抽象 - 向上下文添加新的派生类型 - OnSaveChanges给出错误[已添加具有相同键的项目] [英] Hierarchy - base class abstract - add new derived type to context - OnSaveChanges gives error [An item with the same key has already been added]
问题描述
public abstract class DocumentLink
{
public virtual int DocumentLinkID
{
get;
set;
}
public virtual int ModelID
{
get;
set;
}
}
public class TaskCommunicationDocumentLink : Document.DocumentLink
{
public virtual int? StatusID
{
get;
set;
}
}
  ;
模型构建器定义
modelBuilder.Entity<Document.DocumentLink>().MapHierarchy
(p => new
{
DocumentLinkID = p.DocumentLinkID,
ModelID = p.ModelID
}
).ToTable("Document.DocumentLink");
modelBuilder.Entity<Task.TaskCommunicationDocumentLink>().MapHierarchy
(p => new
{
DocumentLinkID = p.DocumentLinkID,
StatusID = p.StatusID
}
).ToTable("Task.TaskComunicationDocumentLink");
Model.Task.TaskCommunicationDocumentLink tdl = new Model.Task.TaskCommunicationDocumentLink();
tdl.CreatedByContactID = 1;
tdl.UpdatedByContactID = 1;
tdl.DateCreated = DateTime.Now;
tdl.DateUpdated = DateTime.Now;
tdl.ModelFullName = "aaa";
tdl.ModelID = 1;
tdl.StatusID = 111;
context.DocumentLinks.Add(tdl);
context.SaveChanges();
给出错误
在多个位置生成跨实体或关联共享的值。检查映射是否将EntityKey拆分为多个商店生成的列。
A value shared across entities or associations is generated in more than one location. Check that mapping does not split an EntityKey to multiple store-generated columns.
{"已添加具有相同密钥的项目。"}
{"An item with the same key has already been added."}
一旦我删除了DocumentLink的摘要,它就不会再给我错误并保存记录。
As soon as i remove abstract for DocumentLink it does not give me the error any more and save the record.
以下代码有效..
public class DocumentLink
{
public virtual int DocumentLinkID
{
get;
set;
}
public virtual int ModelID
{
get;
set;
}
}
问题:
我仍然希望将DocumentLink作为抽象类。
I still want to have DocumentLink as an abstract class.
推荐答案
感谢您找到这个,这是CTP4中的一个错误,我们将制作确定它得到修复。目前看起来唯一可行的解决方法是从DocumentLink中删除摘要。
Thank you for finding this, it is a bug in CTP4 and we will make sure it gets fixed. For the moment it looks like the only feasible workaround is to remove abstract from DocumentLink.
~Rowan
这篇关于层次结构 - 基类抽象 - 向上下文添加新的派生类型 - OnSaveChanges给出错误[已添加具有相同键的项目]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!