I'm trying to accomplish a simple task of creating a database with 2 tables with a 1:* relationship.
我正在尝试完成一个简单的任务,即创建一个包含2个具有1:*关系的表的数据库。
In one table (Foos) I have
在一张桌子(Foos)中我有
Id
...
...
In the other (Bars) I have
在其他(酒吧)我有
Id
FooId
...
...
...
I created a Foreign Key Relationship from Bars to Foos with the Foriegn Key Table being Bars pointing to the FooId column, and the Primary Key table being Foos pointing to the Id column.
我创建了一个从Bars到Foos的外键关系,其中Foriegn Key Table是指向FooId列的Bars,而主键表是指向Id列的Foos。
I then created my ADO.NET Entity data model and it correctly picked up the Foos and Bars tables and the relationship and creates the navigation properties.
然后我创建了我的ADO.NET实体数据模型,它正确地选取了Foos和Bars表以及关系并创建了导航属性。
Now from within my MVC application I try to get a Foo like this:
现在从我的MVC应用程序中,我尝试得到这样的Foo:
var foo = (from f in _db.Foos where f.Id == id select f).FirstOrDefault();
Then I try to access the Bars property of the foo object and get no results. However if I inspect the _db.Bars set I see the Bars with the FooId set to the correct id.
然后我尝试访问foo对象的Bars属性并且没有得到任何结果。但是,如果我检查_db.Bars集,我会看到将FooId设置为正确id的Bars。
What should I do to troubleshoot why the relationship isn't working? I have looked at many tutorials and the documentation in MSDN for table relationships but nothing is obviously wrong.
我该怎么做才能解决为什么这种关系不起作用?我已经查看了MSDN中的许多教程和文档中的表关系,但没有什么是明显错误的。
I'm trying to use this in a ASP.NET MVC application in case that is relevant.
我试图在ASP.NET MVC应用程序中使用它,以防相关。
1 个解决方案
#1
use an include on the Foos table
在Foos桌上使用包含
var foo = (from f in _db.Foos.Include("Bars") where f.Id == id select f).FirstOrDefault();
var foo =(来自_db.Foos.Include(“Bars”)中的f,其中f.Id == id select f).FirstOrDefault();
#1
use an include on the Foos table
在Foos桌上使用包含
var foo = (from f in _db.Foos.Include("Bars") where f.Id == id select f).FirstOrDefault();
var foo =(来自_db.Foos.Include(“Bars”)中的f,其中f.Id == id select f).FirstOrDefault();