尝试使用面向 .net 3.5 网站的实体框架遍历 SQL 数据库中的表
我只是尝试使用 ado.net 实体框架从两个 sql server 数据库表中获取数据。我的代码是:
using (Model.Entities e = new Model.Entities())
{
return e.PAGE.First().CONTROL;
}
数据库设置有两个表,一个控制表,通过表中的“id”字段(control_id)链接到页表。每个 PAGE 对象都有一个 CONTROL 对象。
我的返回值不断得到空值,我知道这是不对的。
我可以使用 vis studio 和断点来查看“e”中有一个 PAGE 对象,并且可以看到“e”中有多个 CONTROL 对象。这不是一个大型数据库,我只是在那里有一些示例数据以确保我能够正常工作 - 所以我知道应该有一个 CONTROL 对象连接到此页面(我已经通过 sql server 验证了这一点)。
我非常熟悉一般的代码语法,我已经使用 LINQ 几年了;但是,我没有在实体框架或 ado.net 4 上做太多工作。
似乎如果我只是提取单个表数据,那么它就可以正常工作(ieePAGE.First() .. 或 .. e.CONTROL.Where (x=>x.someValue.Equals('someValue') ) 但如果我尝试通过遍历表来进行拉取,那么我什么也得不到(NULL)
。
I'm simply trying to get data from two sql server db tables using ado.net entity framework. My code is:
using (Model.Entities e = new Model.Entities())
{
return e.PAGE.First().CONTROL;
}
The database is setup to have two tables, a control table which links to a page table via an 'id' field in the tables (control_id). There is one CONTROL object for each PAGE object.
I keep getting a null value for my return value and I know that's not right.
I can use vis studio and breakpoints to see that there is a PAGE object in 'e' and I can see that there are multiple CONTROL objects in 'e'. This isn't a large database, I just have some sample data in there to ensure that I get this working - so I know that there should be a CONTROL object connected to this PAGE (i've verified this through sql server).
I am very familiar with the general code syntax, I've been using LINQ for a couple of years; however, I have not done much work at all with the entity framework or ado.net 4.
It seems like if I just pull individual table data then it works fine (i.e. e.PAGE.First() .. or .. e.CONTROL.Where(x=>x.someValue.Equals('someValue') ) but if I try to pull by traversing through the tables then I get nothing back (NULL).
Hope that all makes sense.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有一些问题要问你:
如果以上所有问题的答案都是是,那么这应该可行:
在这里,您将返回第一个“页面”记录,并急切加载关联的控件。
生成的 SQL 应该是这样的:
Some questions for you:
If the answer to all of the above is Yes, then this should work:
Here, you are returning the First "Page" record, and eager loading the associated control.
The SQL produced should be something like this: