2010-04-05 65 views
0

如何在实体框架中设置外键值字段?我有卡特拉实体(与卡特拉表相关)。我需要简单的保存方法这一领域,但是,RehberID,KAmpanyaId,BirimID是foregin关键....如何在Entity Framework的外键字段中添加数据?

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的是未处理的用户代码

link text

+0

Duplicate:http://stackoverflow.com/questions/2577856/if-adding-new-entity-gives-error-me-entitycommandcompilationexception-was-unha – 2010-04-05 16:10:19

回答

1

的您的EntityKey的第一个参数应该是你的上下文的名称,而不是你的名字varaible:

new System.Data.EntityKey("GenSatisModuleEntities.Rehber", "ID", RehberID); 

和Rehber应该是你的EntitySet的名称。

相关问题