2

我有這些實體的屬性(這僅僅是爲這個職位的抽象,我創建):實體框架:指定實體到另一個實體

  • 語言
  • 說明

這些是它們之間的引用:

  • * - 1 語言
  • 描述 * - 1 語言
  • 1 - 1 說明

如果我取這樣的:

var myFetch = from c in context.Districts 
       where c.Id = 10 
       select new { DistrictId = c.Id, Lang = c.Language }; 

,並在這之後,我試着將它分配給說明這樣的:

Description desc = Description.CreateDescription(0, "My description"); 
desc.DistrictReference.EntityKey = new EntityKey("MyEntities.Descriptions", "DistrictId", myFetch.DistrictId); 
desc.Language = myFetch.Lang; //throws error 

拋出的錯誤是:

System.InvalidOperationException:該 關係不能因爲 EntitySet名稱 'MyEntities.Descriptions'是 不是vali d作爲'區' 的關聯集名稱 'MyEntities.District_Description'。

我在做什麼錯?

回答

1

如果myFetch要成爲一個類的實例District你可以以編程方式執行:

desc.DistrictReference.EntityKey = new EntityKey( 
    String.Format( 
    "{0}.{1}", 
    myFetch.EntityKey.EntityContainerName, 
    myFetch.EntityKey.EntitySetName), 
    "DistrictId", 
    myFetch.DistrictId); 
+0

我喜歡這樣 - 它可以節省拼寫錯誤的麻煩 – veljkoz 2010-06-25 12:36:44

2

正是這個消息說:你指定了錯誤的實體集名稱。

  1. 打開您的EDMX。
  2. 打開模型瀏覽器窗口。
  3. 發現在模型瀏覽器區實體
  4. 右鍵單擊它,選擇「屬性」
  5. 注意正確的實體集的名稱
+0

不,情況並非如此。不管怎樣,District不是用在一個字符串裏,所以我不能拼寫它。 – veljkoz 2009-10-01 09:56:16

+0

是的,你還沒有找到它。 *所有* EF名稱最終返回字符串(在EDMX中)。就像我說的,看看映射。描述可能是你想要的EntitySet名稱,但它不是你模型中的內容。 – 2009-10-01 14:11:55

+1

Argh ...我把EntityKey放在了錯誤的一邊......它應該是: desc.DistrictReference.EntityKey = new EntityKey(「MyEntities.Districts」,「Id」,myFetch.DistrictId); 謝謝! – veljkoz 2009-10-01 15:42:11