0
我尝试解决以下:EF代码优先映射一个表到一些实体
在实体HRCard和BPCard我有属性
public int DefaultAddressId { get; set; }
[ForeignKey("DefaultAddressId")]
public AddressDetail Address { get; set; } // table AddressDetail
至今没有问题,现在我的问题: 在BPCard我有另外一个属性:
public virtual ICollection<AddressDetail> Addresses { get; set; } //table AddressDetail
完整的代码如下:
public abstract class EntityBase : IEntityModel {
[Key]
public int EntityId { get; set; }
[Required]
[StringLength(50)]
public string EntityKey { get; set; }
//...
}
// table HRCards
public class HRCard : EntityBase {
//Id from base class
// working fine
//...
public int DefaultAddressId { get; set; }
[ForeignKey("DefaultAddressId")]
public AddressDetail Address { get; set; } // table AddressDetail
}
// table BPCards
public class BPCard : EntityBase {
//Id from base class
// working fine
//...
public int DefaultAddressId { get; set; }
public int DefaultContactId { get; set; }
//working fine
[ForeignKey("DefaultAddressId")]
public AddressDetail DefaultAddress { get; set; } //table AddressDetail
//how can i solve this??
// table AddressDetail
public virtual ICollection<AddressDetail> Addresses { get; set; }
}
public class AddressDetail : EntityBase {
//Id from base class
// working fine
//...
public int ParentId { get; set; }
}
我有很长一段时间寻找,但没有结果真正解决我的问题。我的第一个解决方案是将表分成HRAddress和BPAddress,这工作正常。
编辑: 如果我开始启用迁移我得到一个错误信息:
“属性‘的ParentId’不能被配置为导航属性的属性必须是有效的实体类型和属性应该有一个非抽象的getter和setter。对于集合属性,类型必须实现ICollection,其中T是有效的实体类型。“
千恩万谢
PS: 我可以改变标签后更好的映射?
目前尚不清楚问题是什么或你想达到什么目的。 – Szer
我先使用代码,并且迁移工具运行时出错。 //我该如何解决这个问题? public virtual ICollection Addresses {get;组; } –
FaceOfIngo
@FaceOfIngo哪个错误?编辑它在你的问题请 –