我想创建0..1到0..1的关系,但是,由于某种原因,当尝试插入数据库时,Entity Framework一直告诉我:
“保存未公开其关系的外键属性的实体时发生错误.EntityEntries属性将返回null,因为无法将单个实体标识为异常的来源。保存时可以更轻松地处理异常通过在实体类型中公开外键属性。有关详细信息,请参阅InnerException。“
我的部分代码:
public class Usable {
public int ID { get; set; }
//[ForeignKey("ObjectOfInterest")] -> uncommenting this causes:
//The ForeignKeyAttribute on property 'MentionID' on type
//'MetaDataModeller.Models.Core.Category' is not valid. The navigation property
//'ObjectOfInterest' was not found on the dependent type
//'MetaDataModeller.Models.Core.Category'. The Name value should be a valid
//navigation property name.
//[ForeignKey("Mention")] -> uncommenting this makes no difference
public int? MentionID { get; set; }
public virtual ObjectOfInterest Mention { get; set; }
}
public class ObjectOfInterest {
public int ID { get; set; }
public virtual Usable Use { get; set; }
}
我的DbContext的祖先有这个代码重写OnModelCreating:
modelBuilder.Entity<Usable>()
.HasOptional(usable => usable.Mention)
.WithOptionalDependent(obj => obj.Use);
那么 - 有可能创建0..1到0..1的关系吗?怎么样? / 为什么不? /有没有办法以某种方式解决它?
谢谢!