5

EntityFramework 4.1 kullanıyorum.Varlık Çerçevesi ile birden çok ilişki birden çok

Aşağıdaki modeli vardır:

public class User 
{ 
    [Key] 
    public int Id { get; set; } 
    public string Username { get; set; } 
    public string Password { get; set; } 

    public virtual User Creator { get; set; } 
    public virtual User LastEditor { get; set; } 
} 

ben bu hata var benim veritabanını oluşturmak için deneyin:

public class Course 
{ 
    [Key] 
    public int Id { get; set; } 
    public string Name { get; set; } 

    public virtual User Creator { get; set; } 
    public virtual User LastEditor { get; set; } 
} 

: Ben de bu sınıf var

Unable to determine the principal end of an association between the types 'User' and 'User'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.

Ama Ve tamam çalışıyor, şu yabancı anahtarlar üretildi:

Creator_Id 
LastEditor_Id 

Kullanmak için Kullanıcı modelime ekleyebileceğim herhangi bir şey var mı?

+0

bu soruya bir göz atın ve onu doğru yönde yol açarsa bkz: http://stackoverflow.com User çoğu binada aynı başarmak için Akıcı API ile ilişkileri yapılandırmak zorunda kalacak/questions/6531671/what-does-principal-son-of-bir-ilişki-in-11-ilişki-in-varlık-fr – Cyric297

cevap

7

EF dener - Kongre tarafından - her ikisi de yer kendileri User sınıfı ifade çünkü User.Creator ve User.LastEditor arasında bire-bir ilişki oluşturmak için bu Course sınıf için durum değildir.. Course.Creator ve Course.LastEditor EF için User ile bire çok ilişki oluşturur.

modelBuilder.Entity<User>() 
    .HasOptional(u => u.Creator)  // or HasRequired 
    .WithMany(); 

modelBuilder.Entity<User>() 
    .HasOptional(u => u.LastEditor) // or HasRequired 
    .WithMany(); 
+0

Teşekkürler! İşe yarıyor! Ve güzel bir açıklama. –

İlgili konular