实体框架不按预期运行 [英] Entity Framework Not Operating as Expected
问题描述
我正在使用 http:// msdn.microsoft.com/en-us/data/jj591506 。
using (var db = new DatabaseEntities())
{
// Section 1
var section = new Section
{
SectionID = 1,
SectionLetter = "d"
};
db.Sections.Add(section);
db.SaveChanges();
// Section 2
var query = from b in db.Sections
orderby b.SectionLetter
select b;
foreach (var item in query)
{
this.Title = item.SectionLetter;
}
}
当我使用代码的第1节和第2节在一起他们工作得很好。然而,当我运行第2节时,标题不会改变,这意味着它甚至不返回单个项目,因此不会进入foreach循环。我不明白为什么它不会进入循环。
When I use section 1 and section 2 of the code above together they work well. However, when I run just Section 2 the title does not change which means it does not even return a single item and thus does not enter the foreach loop. I can't see why it would not enter the loop.
我意识到这个问题可能是一个明显的答案,但我认为我应该问stackoverflow而不是问一个同事,所以有同样问题的人可以找到答案。
I realise there is probably an obvious answer to this question but I thought I should ask on stackoverflow rather than ask a colleague so others with the same question could find an answer.
我怀疑这个问题可能有一个线索: System.Data.Entity不能按照MVC实体框架的预期工作。不幸的是,我似乎不能工作。
I suspect there might be a clue in this question: System.Data.Entity not working as expected for MVC Entity Framework. Unfortunately I can't seem to work it out.
推荐答案
它看起来像行 var db = new DatabaseEntities()
正在连接到没有任何行的数据库。检查连接
属性 db
以确保它位于正确的服务器/实例/文件上。
It looks like the line var db = new DatabaseEntities()
is connecting to a database that doesn't have any rows. Check the Connection
property of db
to make sure it is on the correct server/instance/file.
如果您在 query.ToArray()
上执行快速观察,您应该可以看到表的内容。
You should be able to see the contents of the table if you perform a "Quick Watch" on query.ToArray()
.
这篇关于实体框架不按预期运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!