【问题标题】:How to make Entity Key Mapping in Entity Framework like sql's foreign key?如何在实体框架中进行实体键映射,如sql的外键?
【发布时间】:2010-04-07 07:35:36
【问题描述】:

我尝试在我的实体应用程序上提供实体地图。但是我该怎么做呢?我试着让它像下面这样:

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,它们是外键。我应该使用实体键,但如何使用?

【问题讨论】:

  • 我尝试这样做:(来自 genSatisCtx.Kartlar 中的 k,其中 k.RehberReference.EntityKey == RehberID 选择 k);

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


【解决方案1】:
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;

            }
        }
    }


}

【讨论】:

  • 看起来用户拥有的模型将具有导航属性,如果他在网格中使用这些属性,这可能会导致延迟加载杀死性能的问题。如果是这种情况,OP 应该使用急切加载。 codeproject.com/Articles/102647/….
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
相关资源
最近更新 更多