【问题标题】:Entity frame work code first relationship problems实体框架代码优先关系问题
【发布时间】:2016-04-13 11:40:13
【问题描述】:

使用实体框架时,我在表关系方面遇到了一些问题。

它位于 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; }
}

我已经用谷歌搜索了这个并尝试使用数据注释和流畅的 API 来修复它,但我无法摆脱错误。

谁能指出我做错了什么?

谢谢:)

【问题讨论】:

  • 您可以为这两个实体添加您当前的映射吗?

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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-11
    • 2017-07-05
    • 2012-10-26
    • 2021-11-02
    • 2015-03-26
    • 2012-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多