如何在实体框架中像sql的外键一样进行实体密钥映射?

时间:2010-04-07 07:35:37

标签: asp.net visual-studio-2008 entity-framework c#-3.0

我尝试在我的实体应用上提供实体地图。但是我怎么能这样做呢?我尝试如下所示:

var test = ( from k in Kartlar where k.Rehber.....

上面的代码k。(看不到Rehber或不工作)如果你是对的,我可以写k.Rehber.ID和其他人。我不能写:

from
 k in
 Kartlar
where
k.Rehber.ID = 123 //assuming that navigation property name is Rehbar and its primary key of Rehbar table is ID
&& k.Kampanya.ID = 345 //assuming that navigation property name is Kampanya and its primary             //key of Kampanya table is ID
&& k.Birim.ID = 567 //assuming that navigation property name is Birim and its primary key of Birim table is ID
select
k

您可以看到的图片:alt text http://img695.imageshack.us/img695/9334/test1f.png

另外:你应该看看:http://i42.tinypic.com/2nqyyc6.png

我有一个表,它包含3个外键字段:

我的表: Kartlar

  • ID(Pkey)
  • RehberID(Fkey)
  • KampanyaID(Fkey)
  • BrimID(Fkey)
  • 名称
  • 详细

如何使用LINQ编写实体查询?

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

但请小心我看不到RehberID,KampanyaID,BrimID在实体中是外键。我应该使用实体密钥但是如何?

1 个答案:

答案 0 :(得分:0)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Web.UI.MobileControls;


namespace EfEntity
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)<br>
            {
                gwKampanya.DataSource = Kart(297688, 88689, 68686);
                gwKampanya.DataBind();
            }
        }

        public List<Kartlar> Kart(int RehberID, int KampanyaID, int BrimID)
        {
            List<Kartlar> kartlar;
            using (xyzEntities genSatisctx = new xyzEntities())
            {
                kartlar = (from k in genSatisctx.Kartlar
                           where k.Rehber.ID == RehberID &&
                                 k.Kampanya.ID == KampanyaID &&
                                 k.Birim.ID == BrimID
                           select k).ToList();

                return kartlar;

            }
        }
    }


}