简体   繁体   English

如何使用Entity Framework有效选择实体和相关实体的ID

[英]How to effectively select entity and ids of related entities with Entity Framework

I use Database first approach. 我使用数据库优先方法。 I have the following tables in my database: 我的数据库中有以下表格:

[Country](ID, Name)
[Status](ID, Name)
[User](ID, Name)
[Object](ID, StatusID, Name)
[ObjectXCountries](ObjectID, CountryID)
[ObjectXUser](ObjectID, UserID)

Now Entity Framework creates these entities: 现在,实体框架创建以下实体:

Country { ID, Name }
User { ID, Name }
Status { ID, Name }
Object { ID, StatusID, Status, Name, Countries, Users }

To edit a list of objects UI needs the following JSON structure: 要编辑对象列表,UI需要以下JSON结构:

{ 
   cnt: [ { id: 1, name: 'some country', ...}, ],
   usr: [ { id: 1, name: 'some user', ...}, ],
   st: [ { id: 1, name: 'some status', ...}, ],
   objects: [ { id: 1, name: 'some name', st: 1, cnt: [ 1, 2, 3 ], usr: [ 1, 2, 3] }, ... ] 
}

Getting a list of countries, users and statuses is trivial, but how do I get a list of object with ids connected to them? 获取国家,用户和状态的列表很简单,但是如何获取具有ID的对象列表呢? Right now I have the following code, which results in a very inefficient query: 现在,我有以下代码,这将导致效率非常低下的查询:

ctx.Object.Select(rec => new ObjectDTO {
   ID = rec.ID,
   Name = rec.Name,
   StatusID = rec.StatusID,
   UserIDs = rec.Users.Select(usr => usr.ID).ToArray(),
   CountryIDs = rec.Countries.Select(cnt => cnt.ID).ToArray()
});

It translates to sql, which is dependent on the number of objects, it selects all objects and then it creates a select query for each of the objects to get a list of user ids and countries. 它将转换为sql,这取决于对象的数量,选择所有对象,然后为每个对象创建一个选择查询,以获取用户ID和国家/地区的列表。 If I have a hundred objects, it will result in 101 queries. 如果我有一百个对象,它将导致101个查询。 First one to get a list of objects and 100 to get connected ids. 第一个获取对象列表,第100个获取连接的ID。 (UPD: 201 queries, for each entity there are two additional queries to get user ids and countries ids). (UPD:201个查询,对于每个实体,还有两个附加查询以获取用户ID和国家/地区ID)。

Is it possible to select this info using only 3 select queries? 是否可以仅使用3个选择查询来选择此信息? It's easy to do in sql, but I want to know how I can do it in Entity Framework. 在sql中很容易做到,但是我想知道如何在Entity Framework中做到这一点。

UPD: In SQL I would just write 3 select statements: UPD:在SQL中,我只写3条select语句:

SELECT * FROM [Object]
SELECT * FROM [ObjectXCountries]
SELECT * FROM [ObjectXUser]

Call this query from code and then join three result sets on ObjectID. 从代码中调用此查询,然后在ObjectID上连接三个结果集。 Maybe there's some way to make tables [ObjectXCountries] and [ObjectXUser] available in entity context, so I can write a similar code? 也许有某种方法可以使表[ObjectXCountries]和[ObjectXUser]在实体上下文中可用,所以我可以编写类似的代码?

UPD: I use Entity Framework 6.2. UPD:我使用实体框架6.2。

Ok, after googling a lot and trying out different ways, I have found a simple and working solution. 好的,经过大量的搜索并尝试了不同的方式之后,我找到了一个简单且可行的解决方案。

Multiple queries are the result of Lazy Loading. 多个查询是延迟加载的结果。 If I "Include" my navigational properties it will get everything in one query. 如果我“包括”我的导航属性,它将在一个查询中获得所有信息。 This can be achieved with the following code: 这可以通过以下代码实现:

ctx
.Object
.Include("Users")
.Include("Countries")
.Select(rec => new ObjectDTO {
   ID = rec.ID,
   Name = rec.Name,
   StatusID = rec.StatusID,
   UserIDs = rec.Users.Select(usr => usr.ID).ToArray(),
   CountryIDs = rec.Countries.Select(cnt => cnt.ID).ToArray()
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM