entity-framework-core

What is the most effective way to delete entites that satisfy a condition from a database?

本秂侑毒 提交于 2021-01-27 11:23:39
问题 AFAIK, there isn't a direct way to delete entities using a predicate such as DbSet.RemoveWhere(() => {}) I tried a few ways to delete and don't know which is the most efficient way to do it. Could you point me in the right direction? The first and most basic thing I tried is: _context.Users.RemoveRange(_context.Users.Where(u => u.Name.Equals("John"))); which loads the user to the memory before deletion. I don't like this approach. The second way I tried is using the Z.EntityFramework.Plus

What is the most effective way to delete entites that satisfy a condition from a database?

六眼飞鱼酱① 提交于 2021-01-27 11:22:17
问题 AFAIK, there isn't a direct way to delete entities using a predicate such as DbSet.RemoveWhere(() => {}) I tried a few ways to delete and don't know which is the most efficient way to do it. Could you point me in the right direction? The first and most basic thing I tried is: _context.Users.RemoveRange(_context.Users.Where(u => u.Name.Equals("John"))); which loads the user to the memory before deletion. I don't like this approach. The second way I tried is using the Z.EntityFramework.Plus

How to reload collection in EF Core 2.x?

半腔热情 提交于 2021-01-27 10:51:59
问题 I know there is a Load method. _dbContext.Entry(blog).Collection(b => b.Posts).Load() But I'm try to handle concurrency conflicts, I've been add a post into blog.Posts . if call Load , it do not clear the blog.Posts , just append the existing Posts to it. I had try: blog.Posts = null; _dbContext.Entry(blog).Collection(b => b.Posts).Load() But blog.Posts become a empty collection (Zero Count). So I want a Reload . 回答1: Unfortunately although EntityEntry has Reload method, there is no such

EF Core 3.0 - Convert SQL to LINQ

放肆的年华 提交于 2021-01-27 10:49:19
问题 The example given in the blog has the following from e in s.StudentCourseEnrollments where courseIDs.Contains(e.Course.CourseID) select e The contains logic will not work when we are looking for an exact match. If a student has enrolled for 6 courses (ex : 1,2,3,4,5,6) and the requested list contains 5 (ex: 1,2,3,4,5) the query will return a match when it should not. The other way works well when the student has enrolled in a subset of the requested list. Below solution works but need help to

EF Core 3.0 - Convert SQL to LINQ

て烟熏妆下的殇ゞ 提交于 2021-01-27 10:48:29
问题 The example given in the blog has the following from e in s.StudentCourseEnrollments where courseIDs.Contains(e.Course.CourseID) select e The contains logic will not work when we are looking for an exact match. If a student has enrolled for 6 courses (ex : 1,2,3,4,5,6) and the requested list contains 5 (ex: 1,2,3,4,5) the query will return a match when it should not. The other way works well when the student has enrolled in a subset of the requested list. Below solution works but need help to

How to group records and retrieve only first group with top N records

我只是一个虾纸丫 提交于 2021-01-27 08:23:11
问题 I have the following record set ID BatchID ClientName CreatedDateTime ----------- -------------- --------------- ----------------------- 1 NULL B 2018-02-16 19:07:46.320 2 NULL B 2018-02-16 19:07:46.320 3 NULL B 2018-02-16 19:07:46.597 4 NULL B 2018-02-16 19:07:46.597 5 NULL B 2018-02-16 19:10:10.260 6 NULL B 2018-02-16 19:10:10.260 7 NULL B 2018-02-16 19:21:34.303 8 NULL B 2018-02-16 19:21:34.303 9 NULL B 2018-02-16 19:21:44.780 10 NULL B 2018-02-16 19:21:44.780 11 NULL A 2018-02-16 19:24:35

How to group records and retrieve only first group with top N records

耗尽温柔 提交于 2021-01-27 08:20:37
问题 I have the following record set ID BatchID ClientName CreatedDateTime ----------- -------------- --------------- ----------------------- 1 NULL B 2018-02-16 19:07:46.320 2 NULL B 2018-02-16 19:07:46.320 3 NULL B 2018-02-16 19:07:46.597 4 NULL B 2018-02-16 19:07:46.597 5 NULL B 2018-02-16 19:10:10.260 6 NULL B 2018-02-16 19:10:10.260 7 NULL B 2018-02-16 19:21:34.303 8 NULL B 2018-02-16 19:21:34.303 9 NULL B 2018-02-16 19:21:44.780 10 NULL B 2018-02-16 19:21:44.780 11 NULL A 2018-02-16 19:24:35

Change name of generated Join table ( Many to Many ) - EF Core 5

旧城冷巷雨未停 提交于 2021-01-27 07:10:20
问题 How to change name of a join table that EF Core 5 Created ? for example public class Food { public int FoodId { get; set; } public string Name { get; set; } public string Description { get; set; } public string Ingredients { get; set; } public string PhotoPath { get; set; } public ICollection<Menu> Menus { get; set; } } public class Menu { public int MenuId { get; set; } [Column(TypeName = "date")] public DateTime MenuDate { get; set; } public bool IsPublished { get; set; } public ICollection

Change name of generated Join table ( Many to Many ) - EF Core 5

99封情书 提交于 2021-01-27 07:08:01
问题 How to change name of a join table that EF Core 5 Created ? for example public class Food { public int FoodId { get; set; } public string Name { get; set; } public string Description { get; set; } public string Ingredients { get; set; } public string PhotoPath { get; set; } public ICollection<Menu> Menus { get; set; } } public class Menu { public int MenuId { get; set; } [Column(TypeName = "date")] public DateTime MenuDate { get; set; } public bool IsPublished { get; set; } public ICollection

Wire up ExpressionVisitor with EF Core Include

瘦欲@ 提交于 2021-01-27 07:07:15
问题 I have an ExpressionVisitor which I add to EF Core's IQueryable<T> . Everything works fine except the Include methods. Probably because they enforce your IQueryable<T>.Provider to be an EntityQueryProvider . Whenever I try to Include now it results in multiple queries which in turn results in the error "A second operation started on this context before a previous operation completed. Any instance members are not guaranteed to be thread safe.". How can I wire up my ExpressionVisitor so it