【发布时间】:2015-09-30 14:24:12
【问题描述】:
好的,我在这里遗漏了什么,或者这只能通过数据注释来完成?
我有一个文档实体模型,它有一个外键指向添加文档的用户(一对一关系):
[Table("Documents", Schema = "Configuration")]
public class Document : IPrimaryKey {
[Key]
public long Id { get; set; }
[Required]
public string OrginalName { get; set; }
[Required]
public DocumentTypes DocumentType { get; set; }
[Required]
public MIMETypes MIMEType { get; set; }
[Required]
public byte[] Data { get; set; }
[DefaultValue(false)]
public bool IsPublic { get; set; }
[Required]
public DateTimeOffset DateTimeAdded { get; set; }
[Required]
public long AddedByUser { get; set; }
[ForeignKey("AddedByUser")]
public virtual Details Details { get; set; }
}
然后我有一个用户(详细信息)实体,它可以有一个图像文件(存储在文档实体模型中(无|一对一关系):
[Table("Details", Schema = "User")]
public class Details : IPrimaryKey {
[Key]
public long Id { get; set; }
[Required]
public string UserId { get; set; }
[ForeignKey("UserId")]
public AppUser User { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
[CollectionRequired(MinimumCollectionCount = 1)]
public ICollection<Address> Addresses { get; set; }
[CollectionRequired(MinimumCollectionCount = 1)]
public ICollection<Email> Emails { get; set; }
[CollectionRequired(MinimumCollectionCount = 1)]
public ICollection<PhoneNumber> PhoneNumbers { get; set; }
public ICollection<NotificationHistory> NotificationHistory { get; set; }
public long TimeZoneId { get; set; }
public long? ImageId { get; set; }
[ForeignKey("ImageId")]
public virtual Document Document { get; set; }
[ForeignKey("TimeZoneId")]
public virtual TimeZone TimeZone { get; set; }
}
当我尝试创建迁移时出现此错误:
无法确定之间关联的主体端 类型“StACS.PeoplesVoice.DataAccessLayer.EntityModels.User.Details” 和 'StACS.PeoplesVoice.DataAccessLayer.EntityModels.Configuration.Document'。 此关联的主体端必须显式配置 使用关系流式 API 或数据注释。
更新:
虽然仍在研究此问题,但我进行了两项更改并能够绕过错误,但这在我的数据库中产生了意外的结果。
在我添加的文档实体中:
public virtual ICollection<Details> Details { get; set; }
在我添加的详细信息(用户)实体中:
puflic virtual ICollection<Document> Documents { get; set; }
在我的数据库表中,我现在在我想要的字段上有一个外键,但我分别为每个字段设置了一个辅助外键。
我尝试只删除单个虚拟引用,只留下 ICollection 虚拟引用,现在我根本没有外键。
更新 (基于 Akash Kava 建议):
我进行了以下更改 [表(“文档”,架构=“配置”)] 公共类文档:IPrimaryKey { [必需的] 公共字符串原始名称 { 获取;放; }
[Required]
public DocumentTypes DocumentType { get; set; }
[Required]
public MIMETypes MIMEType { get; set; }
[Required]
public byte[] DocumentData { get; set; }
[DefaultValue(false)]
public bool IsPublic { get; set; }
[Required]
public DateTimeOffset DateTimeAdded { get; set; }
[Required]
public long AddedByUser { get; set; }
[Key]
public long Id { get; set; }
[ForeignKey("AddedByUser")]
[InverseProperty("Image")]
public virtual Details User { get; set; }
}
[Table("Details", Schema = "User")]
public class Details : IPrimaryKey {
[Required]
public string UserId { get; set; }
[ForeignKey("UserId")]
public AppUser User { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
[CollectionRequired(MinimumCollectionCount = 1)]
public ICollection<Address> Addresses { get; set; }
[CollectionRequired(MinimumCollectionCount = 1)]
public ICollection<Email> Emails { get; set; }
[CollectionRequired(MinimumCollectionCount = 1)]
public ICollection<PhoneNumber> PhoneNumbers { get; set; }
public ICollection<NotificationHistory> NotificationHistory { get; set; }
public long TimeZoneId { get; set; }
public long? ImageId { get; set; }
[ForeignKey("ImageId")]
[InverseProperty("User")]
public Document Image { get; set; }
[ForeignKey("TimeZoneId")]
public virtual TimeZone TimeZone { get; set; }
[Key]
public long Id { get; set; }
}
我已经注释掉了 Fluent API 代码
无法确定之间关联的主体端 类型“StACS.PeoplesVoice.DataAccessLayer.EntityModels.User.Details” 和 'StACS.PeoplesVoice.DataAccessLayer.EntityModels.Configuration.Document'。 此关联的主体端必须显式配置 使用关系流式 API 或数据注释。
【问题讨论】:
标签: c# entity-framework data-annotations ef-fluent-api