2013-03-10 61 views
5

很多關於我在我的模型3類,你可以看到下面。定義許多在代碼中第一個實體框架

[Table("UserProfile")] 
public class UserProfile 
{ 
    [Key] 
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] 
    public int Id { get; set; } 

    public string UserName { get; set; } 

    public ICollection<MartialArtUserProfile> MartialArtUserProfiles { get; set; } 
} 

[Table("MartialArt")] 
public class MartialArt 
{ 
    [Key] 
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] 
    public int Id { get; set; } 

    public string Name { get; set; } 

    public string Description { get; set; } 

    public string IconPath { get; set; } 

    public string ImagePath { get; set; } 

    public ICollection<MartialArtUserProfile> MartialArtUserProfiles { get; set; } 
} 

public class MartialArtUserProfile 
{ 
    public int UserProfileId { get; set; } 
    public UserProfile UserProfile { get; set; } 

    public int MartialArtId { get; set; } 
    public MartialArt MartialArt { get; set; } 
} 

而且我有一個配置類爲多對多的關係如下圖所示:

public class MartialArtUserProfileConfiguration : EntityTypeConfiguration<MartialArtUserProfile> 
{ 
    public MartialArtUserProfileConfiguration() 
    { 
     HasKey(a => new { a.MartialArtId, a.UserProfileId }); 

     HasRequired(a => a.MartialArt) 
      .WithMany(s => s.MartialArtUserProfiles) 
      .HasForeignKey(a => a.MartialArtId) 
      .WillCascadeOnDelete(false); 

     HasRequired(a => a.UserProfile) 
      .WithMany(p => p.MartialArtUserProfiles) 
      .HasForeignKey(a => a.UserProfileId) 
      .WillCascadeOnDelete(false); 
    } 
} 

定義我的實體的關係後,當我嘗試在軟件包管理器運行更新,數據庫控制檯它說:

模型生成期間檢測到一個或多個驗證錯誤:

\ tSystem.Data.Entity.Edm.EdmEntityType:的EntityType 'MartialArtUserProfile' 沒有定義鍵。定義此EntityType的關鍵字。 \ tSystem.Data.Entity.Edm.EdmEntitySet:的EntityType:EntitySet的 'MartialArtUserProfiles' 是基於類型 'MartialArtUserProfile' 不具有定義的鍵。

我在做什麼錯?

由於提前,

+0

你真的需要MartialArtUserProfile類還是隻是用它來建立多對多的關係? – 2013-03-10 23:43:31

+0

我只是用它來建立多對多的關係。 @Sniffer – anilca 2013-03-11 09:38:59

回答

8

如果我明白你只是想創建一個多對多的一個傳遞表。如果是這樣的話,這是另一種解決方法。使用Fluent API映射如下。您可以將UserProfileToMartialArt更改爲任何您想要的表名稱。 EF不會創建MartialArtUserProfile模型,而是讓EF爲您創建中間立場。這也指定了你的密鑰,這應該讓你圍繞錯誤。

modelBuilder.Entity<UserProfile>() 
    .HasMany(b => b.MartialArts) 
    .WithMany(a => a.UserProfiles) 
    .Map(m => m.MapLeftKey("MartialArtId") 
     .MapRightKey("UserProfileId") 
     .ToTable("UserProfileToMartialArt")); 

在武術模型把

public IList<UserProfile> UserProfiles { get; set; } 

在用戶配置模式把

public IList<MartialArt> MartialArts { get; set; } 
+1

我已經得到評審的反饋,在使用流利.MAP定義上述關係,是不是最好的做法,因爲該解決方案可以改變後進行編譯,並且它不會是直到運行時,這是顯而易見的。你會建議使用反射,或其他方式(從我的頭頂,分配實體表的屬性和映射到常數?) – 2016-07-28 04:04:31

6

試着做這樣的:

[Table("UserProfile")] 
public class UserProfile 
{ 
    [Key] 
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] 
    public int Id { get; set; } 

    public string UserName { get; set; } 

    [InverseProperty("UserProfiles")] 
    public IList<MartialArt> MartialArts { get; set; } 
} 

[Table("MartialArt")] 
public class MartialArt 
{ 
    [Key] 
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] 
    public int Id { get; set; } 

    public string Name { get; set; } 

    public string Description { get; set; } 

    public string IconPath { get; set; } 

    public string ImagePath { get; set; } 

    [InverseProperty("MartialArts")] 
    public IList<UserProfile> UserProfiles { get; set; } 
} 
+0

這對我很好。雖然我處在一個場景中,我只關心*一個實體與另一個實體相關聯。這有可能使用這個?起初我只是有一個IList的其他{獲得;設置;}但它不能正常工作 – Mzn 2013-11-18 12:48:09

+0

你說的是1對1間的關係? – 2013-11-18 15:17:58

+0

我正在談論一種(單向)多關係...單向我的意思是在我的代碼中我想要一個導航屬性。這根本不是什麼大問題,我只是想知道如何儘可能地使用 – Mzn 2013-11-21 17:36:50

5

在6.1的EntityFramework,你不需要做任何的本 - 只需添加兩個集合類型到每個類和一切都落到了位置。

public class UserProfile { 
    public int Id { get; set; } 
    public string UserName { get; set; } 
    public virtual ICollection<MartialArt> MartialArts { get; set; } 

    public UserProfile() { 
     MartialArts = new List<MartialArt>(); 
    } 
} 

public class MartialArt { 
    public int Id { get; set; } 
    public string Name { get; set; } 
    // *snip* 
    public virtual ICollection<UserProfile> UserProfiles { get; set; } 

    public MartialArt() { 
     UserProfiles = new List<UserProfile>(); 
    } 
} 
+0

真棒,爲我工作。這是應該的。走6.1 – parliament 2014-09-26 21:38:45

+0

如果這是Admir的回答以上(http://stackoverflow.com/a/15328042/489396)和「落入地方」是根據慣例,需要映射表之間有什麼約定的演變? – 2015-09-19 14:47:45

+1

@GusCrawford - 映射表被保持完全從代碼隱藏的,它將被簡單地通過具有在代碼相對對象的兩個集合創建。 – 2015-09-21 10:09:16

相關問題