实体框架工作代码第一关系问题

时间:2016-04-13 11:40:14

标签: database entity-framework data-annotations ef-fluent-api

使用实体框架我遇到了表关系的一些问题。

它位于两个表Employee和AspNetUser之间。 我收到以下错误:

无法确定类型“DB.EmployeeData”和“DB.AspNetUser”之间关联的主要结尾。必须使用关系流畅API或数据注释显式配置此关联的主要结尾。我的poco对象可以在下面看到:

public partial class EmployeeData
{
    public int Id { get; set; }
    public string FullName { get; set; }
    public string PostalCode { get; set; }
    public string City { get; set; }
    public double Longitude { get; set; }
    public double Latitude { get; set; }
    public int Age { get; set; }
    public bool HighlightedNotificationsEnabled { get; set; }
    public System.DateTime LastNotificationTime { get; set; }
    public string StreetAndHouse { get; set; }
    public string Country { get; set; }
    public string Gender { get; set; }
    public bool MatchingNotificationsEnabled { get; set; }
    public bool NearbyNotificationsEnabled { get; set; }
    public string VideoCVLink { get; set; }
    public string FacebookUrl { get; set; }
    public string InstagramUrl { get; set; }
    public string LinkedInUrl { get; set; }
    public string DriveUrl { get; set; }
    public virtual AspNetUser AspNetUser { get; set; }
    public virtual Translation Translation { get; set; }
}

public partial class AspNetUser
{
    public AspNetUser()
    {
        this.AspNetUserClaims = new HashSet<AspNetUserClaim>();
        this.AspNetUserLogins = new HashSet<AspNetUserLogin>();
        this.AspNetRoles = new HashSet<AspNetRole>();
        this.JobOffers = new HashSet<JobOffer>();
        this.EmployeeQualifications = new HashSet<EmployeeQualification>();
        this.NotifiedJobOffers = new HashSet<JobOffer>();
        this.UserDevices = new HashSet<UserDevice>();
        this.Applications = new HashSet<Application>();
    }

    public string Id { get; set; }
    public string Email { get; set; }
    public bool EmailConfirmed { get; set; }
    public string PasswordHash { get; set; }
    public string SecurityStamp { get; set; }
    public string PhoneNumber { get; set; }
    public bool PhoneNumberConfirmed { get; set; }
    public bool TwoFactorEnabled { get; set; }
    public Nullable<System.DateTime> LockoutEndDateUtc { get; set; }
    public bool LockoutEnabled { get; set; }
    public int AccessFailedCount { get; set; }
    public string UserName { get; set; }
    public string JobnetOrganisationId { get; set; }

    public virtual ICollection<AspNetUserClaim> AspNetUserClaims { get; set; }
    public virtual ICollection<AspNetUserLogin> AspNetUserLogins { get; set; }
    public virtual ICollection<AspNetRole> AspNetRoles { get; set; }
    public virtual EmployerData EmployerData { get; set; }
    public virtual EmployeeData EmployeeData { get; set; }
    public virtual ICollection<JobOffer> JobOffers { get; set; }
    public virtual ICollection<EmployeeQualification> EmployeeQualifications { get; set; }
    public virtual ICollection<JobOffer> NotifiedJobOffers { get; set; }
    public virtual ICollection<UserDevice> UserDevices { get; set; }
    public virtual ICollection<Application> Applications { get; set; }
}

我已经用Google搜索并试图使用数据注释和流畅的API修复它,但我无法摆脱错误。

有谁可以指出我做错了什么?

Thx:)

1 个答案:

答案 0 :(得分:1)

看来你有两个问题。首先,您没有在任何一个表上定义主键。因此,将Key属性添加到Id属性中。第二个是,由于两个对象之间存在一对一的关系,因此需要定义主体。在你的情况下:

modelBuilder.Entity<EmployeeData>()
        .HasOptional(e => e.AspNetUser)
        .WithRequired(a => a.EmployeeData);