简体   繁体   中英

How can I add data in foreign key field in Entity Framework?

How can I give set foreign key value field in Entity Framework. I have Kartlar entity (related from kartlar table). I need simple save method this field but, RehberID, KAmpanyaId,BirimID is foregin Key....

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();
        }
    }
}

But it throws to me : ArgumentException was unhandled by user code

link text

The first parameter of your EntityKey should be your context name, not your varaible name:

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

and Rehber should be the name of your EntitySet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM