这是正确的方案:
实体文档继承自Material。 Material具有MaterialSourceLanguage的集合,该集合是多对多的。
public class BaseEntity : IDeletable
{
public virtual Guid Id { get; set; }
public virtual int Version { get; set; }
public virtual bool IsDeleted { get; set; }
public virtual DateTime CreationDate { get; set; }
public virtual DateTime? UpdateDate { get; set; }
}
public class Material : BaseEntity
{
public virtual IList<MaterialSourceLanguage> SourceLanguages { get; set; }
public Material()
{
this.SourceLanguages = new List<MaterialSourceLanguage>();
}
}
public class Document : Material
{
public virtual string FileName { get; set; }
}
public class MaterialSourceLanguage : BaseEntity
{
public virtual Guid? MaterialId { get; set; }
public virtual Material Material { get; set; }
public virtual Guid? LanguageId { get; set; }
public virtual Language Language { get; set; }
public MaterialSourceLanguage()
{
}
}
如果我创建实体Document
并向其附加多个MaterialSourceLanguage
,则会发生外键错误。为什么?
在我们的例子中,真实实体类型为Document
(AddToGraph
中添加的类型)
当Breeze
执行FindInSaveMap
(NHRelationshipFixer.cs
)时,它找不到任何类型Material
的实体。
我希望有足够的信息?
修改:我在NHRelationshipFixer.cs
做了一个解决方法,现在对我们来说已经足够了:
private EntityInfo FindInSaveMap(Type entityType, object entityId)
{
var entityIdString = entityId.ToString();
var entityInfoList = saveMap.Where(p => entityType.IsAssignableFrom(p.Key)).Select(p => p.Value).FirstOrDefault();
if (entityInfoList != null)
{
var meta = session.SessionFactory.GetClassMetadata(entityType);
foreach (var entityInfo in entityInfoList)
{
var entity = entityInfo.Entity;
var id = meta.GetIdentifier(entity, EntityMode.Poco);
if (id != null && entityIdString.Equals(id.ToString()))
return entityInfo;
}
}
return null;
}