实体框架代码优先关系问题

Entity frame work code first relationship problems

使用 entity framework 我遇到了 table 关系的一些问题。

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

无法确定类型 '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; }
}

我用谷歌搜索了这个并尝试使用数据注释和流利的方式修复它 API 但我无法消除错误。

谁能指出我做错了什么?

谢谢:)

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

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