使用实体框架查询 [英] Querying using Entity Framework

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

问题描述

如何查询映射到相应数据库表的实体以检索某些数据?



我有一个或者映射到数据库表的User实体用户,属性userID和用户名。



现在,我想知道通过提供userID来检索UserName的可能方法是什么。



我尝试使用下面的代码,但我不明白为什么它会显示datacontext不存在的错误。



How to query an entity that is mapped to the corresponding database table to retrieve some data?

I have a User entity which OR mapped to the database table User, which attributes userID and username.

Now, I wanted to know what are the possible ways to retrieve the the UserName by providing the userID.

I tried using the following code , but I dont understand why is it displaying an error that the datacontext does not exist.

var CurrentUserName = from c in datacontext.user
                      where c.UserID == someUserID
                      select c.Name;

推荐答案

您需要在使用之前定义datacontext:



You need to define the datacontext before using it:

datacontext = new MyEntities();





当您向项目中添加ADO.NET实体数据模型时,会创建MyEntities(您可以根据需要为其命名)。



MyEntities is created when you add an "ADO.NET Entity Data Model" to your project (you can name it whatever you want).


在下面的链接上尝试实体框架教程。非常好

http://www.entityframeworktutorial.net [ ^ ]
Try entity framework tutorial on below link. Very nice
http://www.entityframeworktutorial.net[^]


这篇关于使用实体框架查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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