实体框架 4:选择单个记录 [英] Entity Framework 4: Selecting Single Record

查看:37
本文介绍了实体框架 4:选择单个记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正计划将我的手动查询编写"代码切换到一个不错的 SQL 框架,这样我就可以将查询或 sql 的事情留给框架,而不是自己编写查询.

I'm currently planning on switching my "manual query-writing" code to a nice SQL framework, so I can leave the queries or sql things to the framework, instead of writing the queries myself.

现在我想知道如何从 Entity Framework 4 的表中获取单个记录?

Now I'm wondering how I can get a single record from my table in Entity Framework 4?

我主要使用像 SELECT * FROM {0} WHERE Id = {1} 这样的 SQL.就我而言,这在 EF4 中不起作用.

I've primarily used SQL like SELECT * FROM {0} WHERE Id = {1}. That doesn't work in EF4, as far as I'm concerned.

有没有办法从我的上下文中选择一个基于 ID 的记录?

Is there a way I can select a single ID-Based record from my Context?

类似于:

public Address GetAddress(int addressId)
{
    var result = from Context.Addresses where Address.Id = addressId;

    Address adr = result as Address;

    return Address;
}

谢谢!

推荐答案

var address = Context.Addresses.First(a => a.Id == addressId);

这篇关于实体框架 4:选择单个记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆