我有两节课
public class Document { public virtual int Id { get; set; } public virtual IListFiles { get; set; } } public class File { public virtual int Id { get; protected set; } public virtual Document Document { get; set; } }
具有以下约定:
public class HasManyConvention : IHasManyConvention { public bool Accept(IOneToManyPart target) { return true; } public void Apply(IOneToManyPart target) { target.Cascade.All(); } }
和这些映射覆盖
public class DocumentMappingOverride : IAutoMappingOverride{ public void Override(AutoMap mapping) { mapping.HasMany(x => x.Files) .Inverse() // this line has no effect .Cascade.AllDeleteOrphan(); } } public class FileMappingOverride : IAutoMappingOverride { public void Override(AutoMap mapping) { mapping.References(x => x.Document).Not.Nullable(); } }
我知道我需要为Document创建一个IClassConvention来改变级联行为,但是我不能让它工作!如果我这样做:
public class DocumentConvention : IClassConvention { public bool Accept(IClassMap target) { return target.EntityType == typeof(Document); } public void Apply(IClassMap target) { target.SetAttribute("cascade", "all-delete-orphan"); } }
我得到:"'cascade'属性未被声明."
如果我这样做:
public class DocumentConvention : IClassConvention { public bool Accept(IClassMap target) { return target.EntityType == typeof(Document); } public void Apply(IClassMap target) { target.HasMany(x => x.Files) .Inverse() .Cascade.AllDeleteOrphan(); } }
然后我得到:
"Duplicate collection role mapping Document.Files"
所以我补充说:
mapping.IgnoreProperty(x => x.Files);
到我的文档映射,但然后文件始终为空.我究竟做错了什么?如何覆盖单个HasMany关系的级联规则?谢谢Andrew
PS对不起,用交叉后这个,但我需要得到这个尽快解决.