简体   繁体   中英

How to write linq query in Entity Framework but my table includes foreign key?

I have a table it includes 3 foreign key field like that:

My Table:

  • ID (Pkey)
  • RehberID (Fkey)
  • KampanyaID (Fkey)
  • BrimID (Fkey)
  • Name
  • Detail

How can i write entity query with linq :?

select * from Kartlar where RehberID=123 and KampanyaID=345 and BrimID=567

BUT please be careful I can not see RehberID, KampanyaID, BrimID in entity they are foreign keys. I should use entity key but how?

My Entity:

Addding data into kartlar 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?

genSatisCtx.AddToKartlar(kartlar);
genSatisCtx.SaveChanges();
int newKartlarID = kartlar.KartID;//kartID or whatever you call your primary key
return newKartlarID;

after you save the object kartlar it gets a primary key that can be used afterwords hopefully this helps and also what language is kartlar????

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