0

i am new in LINQ so i have been following some tutorials and official docs like How to: Map Database Relationships on Developer Network.

I am performing this:

1) Got two classes Locality and Region, there's a one to many relation between this "tables" so than one Region has multiple Localities. I mapped the association like this:

Region:

private EntitySet<City> _cities = new EntitySet<City>();

[Association(Storage = "_cities", ThisKey = "RegionId", OtherKey = "RegionId")]
public EntitySet<City> Cities
{
    get { return _cities; }
    set { _cities.Assign(value); }
} 

The Region has two more fields RegionId and Name. City has two fields too: CityId, Name (beside the RegionId foreign key of course).

Now i populate the database. So i am able to select all cities using a query like the following:

var city = from City cities in db.cities
           select cities;

And i can see all the properties belonging to City Entity. But when i perform this query:

var regiones = from Region region in db.regions
               select region;

I only can access to RegionId,Name because the Cities EntitySet is always empty. I dont now what i am doing wrong, so i hope some of you could give me a hand.

4

2 回答 2

0

您确定在您的上下文中启用了 LazyLoading。如果没有,您可以打开它,或者您可以使用 db.regions.Include("Cities") 显式加载 Cities。

于 2013-09-17T12:06:12.553 回答
0

抱歉浪费你们的时间,我终于发现了我的错误,我用标志 IsDbGenerated = true 定义了 Region 的主键,但是我正在向 City 中的外键发送一个准备好的值,所以关系被打破了,我修复了区域主键和瞧。

于 2013-09-17T14:59:26.417 回答