开发者

Adding data into tables includes foreign keys but which new row created Entity Framework?

Addding data into kartla开发者_JS百科r table (RehberID,KampanyaID,BrimID) is ok. But which Kart'ID created? I need to learn which Id created after adding data (RehberID,KampanyaID,BrimID) into Kartlar?

public static List<Kartlar> SaveKartlar(int RehberID, int KampanyaID, int BrimID, string Notlar)
{
    using (GenSatisModuleEntities genSatisCtx = new GenSatisModuleEntities())
    {
            Kartlar kartlar = new Kartlar();
            kartlar.RehberReference.EntityKey = new System.Data.EntityKey("GenSatisModuleEntities.Rehber", "ID", RehberID);
            kartlar.KampanyaReference.EntityKey = new System.Data.EntityKey("GenSatisModuleEntities.Kampanya", "ID", KampanyaID);
            kartlar.BirimReference.EntityKey = new System.Data.EntityKey("GenSatisModuleEntities.Birim", "ID", BrimID);
            kartlar.Notlar = Notlar;
            genSatisCtx.AddToKartlar(kartlar);
            genSatisCtx.SaveChanges();
            List<Kartlar> kartAddedPatient;
            kartAddedPatient = (from k in genSatisCtx.Kartlar
                                where k.RehberReference.EntityKey == RehberID &&
                                      k.KampanyaReference.EntityKey == KampanyaID &&
                                      k.BirimReference.EntityKey == BrimID
                                select k)
      return kartAddedPatient ;
    }
}

How can I do that? I want to get data from Kartlar which data I added?


Upon you call ObjectContext.SaveChanges the generated ID will be in "kartlar" object:

using (GenSatisModuleEntities genSatisCtx = new GenSatisModuleEntities())
            {
                Kartlar kartlar = new Kartlar();
                //Do your stuff with kartlar...
                genSatisCtx.AddToKartlar(kartlar);
                genSatisCtx.SaveChanges();

                //kartlar.ID - the generated identifier should be loaded into ID property
            }

[updated]

List<Kartlar> kartAddedPatient;
 kartAddedPatient = (from k in genSatisCtx.Kartlar
                                    where k.Rehber.ID == RehberID &&
                                          k.Kampanya.ID == KampanyaID &&
                                          k.Birim.ID == BrimID
                                    select k).ToList()
 return kartAddedPatient ;

Regards,

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜