Scenario I am playing MVC NerdDinner project and using ado.net entity data model instead on 'dbml'
场景我正在玩MVC NerdDinner项目并使用ado.net实体数据模型代替'dbml'
I have 2 database tables Dinner & RSVP where RSVP contains DinnerID as foreign key from Dinner table.
我有2个数据库表Dinner和RSVP,其中RSVP包含DinnerID作为来自餐桌的外键。
Now when I am accessing a particular record from Dinner table, it returns a dinner object with RSVP property but there are no data with that property inspite of RSVP table having data with foreign key from Dinner table
现在,当我从Dinner表访问特定记录时,它返回一个具有RSVP属性的晚餐对象,但是尽管RSVP表具有来自Dinner表的外键数据,但没有该属性的数据
Data
DinnerTable
ID :1
Title : '.Net Architecture'
ID:1标题:'。Net Architecture'
RSVPTable
ID :1
Dinner ID: 1
AttendeeName : 'Miral'
ID:1晚餐ID:1 AttendeeName:'Miral'
ID :2
Dinner ID: 1
AttendeeName : 'Shivani'
ID:2晚餐ID:1 AttendeeName:'Shivani'
So when fetching Dinner data which should return its child RSVP data, I am getting RSVP property with 0 records.
因此,当获取应该返回其子RSVP数据的Dinner数据时,我获得了具有0条记录的RSVP属性。
2 个解决方案
#1
EF is a little different from LINQ. In your query, add something like
EF与LINQ略有不同。在您的查询中,添加类似的内容
var dinner = context.Dinners.First(d => d.DinnerID = id).Include("Attendees");
This will tell EF to attach the associated Attendees objects in a single fetch, doing all of the necessary joins for you.
这将告诉EF在一次获取中附加相关的Attendees对象,为您执行所有必要的连接。
#2
Correct syntax
Table : 'Dinner' & 'RSVP'
表:'晚餐'和'回复'
var dinner =_nerdDinnerEntities.Dinner.Include("RSVP").Where(dd => dd.ID == ID).FirstOrDefault();
var dinner = _nerdDinnerEntities.Dinner.Include(“RSVP”)。其中(dd => dd.ID == ID).FirstOrDefault();
Your require to write Include before FirstOrDefault.
您需要在FirstOrDefault之前写入Include。
'Include' is a method on Entity over here 'Dinner',and it includes table name containing the foreign key i.e. 'RSVP' and the property 'AttendeeName'.
'Include'是实体在这里'Dinner'的方法,它包括包含外键的表名,即'RSVP'和属性'AttendeeName'。
I tried passing one of the property 'AttendeeName' but it didnt worked.
我尝试传递其中一个属性'AttendeeName',但它没有用。
#1
EF is a little different from LINQ. In your query, add something like
EF与LINQ略有不同。在您的查询中,添加类似的内容
var dinner = context.Dinners.First(d => d.DinnerID = id).Include("Attendees");
This will tell EF to attach the associated Attendees objects in a single fetch, doing all of the necessary joins for you.
这将告诉EF在一次获取中附加相关的Attendees对象,为您执行所有必要的连接。
#2
Correct syntax
Table : 'Dinner' & 'RSVP'
表:'晚餐'和'回复'
var dinner =_nerdDinnerEntities.Dinner.Include("RSVP").Where(dd => dd.ID == ID).FirstOrDefault();
var dinner = _nerdDinnerEntities.Dinner.Include(“RSVP”)。其中(dd => dd.ID == ID).FirstOrDefault();
Your require to write Include before FirstOrDefault.
您需要在FirstOrDefault之前写入Include。
'Include' is a method on Entity over here 'Dinner',and it includes table name containing the foreign key i.e. 'RSVP' and the property 'AttendeeName'.
'Include'是实体在这里'Dinner'的方法,它包括包含外键的表名,即'RSVP'和属性'AttendeeName'。
I tried passing one of the property 'AttendeeName' but it didnt worked.
我尝试传递其中一个属性'AttendeeName',但它没有用。