0

Mit dem Entity Framework habe ich einige Probleme mit Tabellenrelationen.Entity Frame Arbeitscode erste Beziehung Probleme

Es ist zwischen den beiden Tabellen, Employee und AspNetUser. Ich erhalte den folgenden Fehler:

Das Prinzipalende einer Zuordnung zwischen den Typen 'DB.EmployeeData' und 'DB.AspNetUser' konnte nicht ermittelt werden. Das prinzipielle Ende dieser Zuordnung muss explizit konfiguriert werden, indem entweder die fließende Beziehung API oder Datenanmerkungen verwendet werden. Mein poco Objekt kann unten gesehen werden:

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; } 
} 

ich dies habe gegoogelt und versucht, es zu beheben unter Verwendung von Daten Anmerkungen und fließend API, aber ich kann der Fehler loszuwerden.

Kann jemand darauf hinweisen, was ich falsch mache?

Thx :)

+0

Können Sie Ihre aktuelle Zuordnung für diese 2 Einheiten hinzufügen? –

Antwort

1

Es scheint, dass Sie zwei Probleme haben. Die erste besteht darin, dass Sie in beiden Tabellen keinen Primärschlüssel definiert haben. Fügen Sie also das Attribut Key zu Ihren Id Eigenschaften hinzu. Die zweite besteht darin, dass Sie eine Eins-zu-Eins-Beziehung zwischen den beiden Objekten haben müssen, um den Principal zu definieren. In Ihrem Fall wäre das:

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

Vielen Dank, gute Erklärung/Beispiel :) – Diemauerdk

Verwandte Themen