简体   繁体   English

如何在Entity Framework中编写linq查询,但我的表包含外键?

[英]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: 我有一个表,其中包含3个外键字段,例如:

My Table: Kartlar 我的桌子: 卡拉特

  • ID (Pkey) ID(Pkey)
  • RehberID (Fkey) RehberID(Fkey)
  • KampanyaID (Fkey) KampanyaID(Fkey)
  • BrimID (Fkey) BrimID(Fkey)
  • Name 名称
  • Detail 详情

How can i write entity query with linq :? 如何使用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. 但是请注意,在实体中我看不到RehberID,KampanyaID,BrimID是外键。 I should use entity key but how? 我应该使用实体密钥,但是如何使用?

My Entity: 我的实体:

Addding data into kartlar Table (RehberID, KampanyaID, BrimID) is ok. 可以将数据添加到kartlar表(RehberID,KampanyaID,BrimID)。 But which Kart'ID created? 但是,哪个Kart'ID创建了? I need to learn which Id created after adding data (RehberID,KampanyaID,BrimID) into Kartlar? 在将数据(RehberID,KampanyaID,BrimID)添加到Kartlar之后,我需要学习创建哪个ID?

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? 我想从Kartlar获取数据,我添加了哪些数据?

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???? 在保存对象kartlar之后,它会获得一个主键,该键可以在后记中使用,希望这会有所帮助,并且kartlar是什么语言?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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