Reputation: 5948
Using the entity framework i am experiencing some problems with table relations.
It is between the two tables, Employee and AspNetUser. I get the following error:
Unable to determine the principal end of an association between the types 'DB.EmployeeData' and 'DB.AspNetUser'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations. My poco object can be seen below:
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; }
}
I have googled this and tried to fix it using data annotations and fluent API but i cant get rid of the error.
Can anyone point out what i am doing wrong?
Thx :)
Upvotes: 0
Views: 47
Reputation: 4048
It seems you have two issues. The first is that you have not defined a primary key on either table. So add the Key
attribute to your Id
properties. The second is that as you have a one-to-one relationship between the two objects you need to define the principal. In your case that would be:
modelBuilder.Entity<EmployeeData>()
.HasOptional(e => e.AspNetUser)
.WithRequired(a => a.EmployeeData);
Upvotes: 1