![](/img/trans.png)
[英]How to add composite foreign key in data annotation for Entity Framework 6
[英]How can I add data in foreign key field in Entity Framework?
如何在Entity Framework中提供設置外鍵值字段。 我有Kartlar實體(與kartlar表有關)。 我需要此字段的簡單保存方法,但是RehberID,KAmpanyaId,BirimID是foregin密鑰。
public static class SatisServicesUpdateTables
{
public static void SaveKartlar(int RehberID, int KampanyaID, int BrimID)
{
using (GenSatisModuleEntities genSatisKampanyaCtx = new GenSatisModuleEntities())
{
Kartlar kartlar = new Kartlar();
kartlar.RehberReference.EntityKey = new System.Data.EntityKey("genSatisKampanyaCtx.Rehber", "ID", RehberID);
kartlar.KampanyaReference.EntityKey = new System.Data.EntityKey("genSatisKampanyaCtx.Kampanya", "ID", KampanyaID);
kartlar.BirimReference.EntityKey = new System.Data.EntityKey("genSatisKampanyaCtx.Birim", "ID", BrimID);
kartlar.Notlar = "hfhfhfhfhfhfghfghfgfghk";
genSatisKampanyaCtx.AddToKartlar(kartlar);
genSatisKampanyaCtx.SaveChanges();
}
}
}
但它拋出了我:用戶代碼未處理ArgumentException
EntityKey的第一個參數應該是您的上下文名稱,而不是可變名稱:
new System.Data.EntityKey("GenSatisModuleEntities.Rehber", "ID", RehberID);
Rehber應該是您的EntitySet的名稱。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.