entity-framework-core

Optimize EF Core query with Include()

我只是一个虾纸丫 提交于 2020-08-05 09:24:29
问题 I have following query within my project and it is consuming lot of time to execute. I am trying to optimize it, but not able to successfully do it. Any suggestions would be highly appreciated. _context.MainTable .Include(mt => mt.ChildTable1) .Include(mt => mt.ChildTable1.ChildTable2) .Include(mt => mt.ChildTable3) .Include(mt => mt.ChildTable3.ChildTable4) .SingleOrDefault( mt => mt.ChildTable3.ChildTable4.Id == id && mt.ChildTable1.Operation == operation && mt.ChildTable1.Method = method &

Filtering with EF Core 2.1 inheritance

限于喜欢 提交于 2020-08-05 06:52:54
问题 I'm trying to find a way to filter my results in EF Core 2.1, when using inherited objects. I've got a base model and several inherited classes (but I've just included one): public class Like { public int Id { get; set; } public LikeType LikeType { get; set; } } public class DocumentLike : Like { [ForeignKey(nameof(Document))] public int DocumentId { get; set; } public virtual Document Document { get; set; } } LikeType is an enum which is defined as the discriminator in the dbcontext. Every

EF Core - Error when adding a related entity

烂漫一生 提交于 2020-08-04 11:49:05
问题 I get an error when I try to update a related entity of an entity that I already got from database. For illustration purposes I have these entites: class Car { int Id ..; string Name ..; virtual ICollection<TireCar> tires ...; } class TireCar { int Id ..; int TireId ..; int CarId..; int Size..; virtual TireBrand tire; virtual Car car; } class TireBrand { int Id; string Name ..; } So, I'm trying to make a Patch method that allows me to update the Car data and also adds, updates or deletes the

EF Core 2.1 - Duplicate relationships when use fluent API

|▌冷眼眸甩不掉的悲伤 提交于 2020-08-03 09:23:08
问题 I develop a web API with .NET Core 2.1 and EF Core. But i have a problem with my ApplicationDbContext that i don't understand. I have 2 tables (Users and Profiles, one to many), the foreign key profile in User table is not required. With Fluent API, when i declare my relationships, ef core create duplicate foreign key on the same reference, but why ? Here is my userModel : public class UserModel { public long Id { get; set; } public string Username { get; set; } public string Password { get;

The entity type 'IdentityUser' is part of a hierarchy, but does not have a discriminator value configured

拟墨画扇 提交于 2020-08-03 05:55:13
问题 I'm trying to migrate a .NET Core 1.1 MVC-application using EntityFrameworkCore (1.0.1) from SQLite ( "Microsoft.EntityFrameworkCore.Sqlite": "1.0.1" ) to MySQL ( "MySql.Data.EntityFrameworkCore": "7.0.6-IR31" ). My context is extending IdentityDbContext , and when running Database.EnsureCreated() , using Sqlite, the AspNet-tables (RoleClaims, Roles, Users, etc) were automatically configured. With the MySQL-connector, when i attempt to run either Update-Database or Database.EnsureCreated() ,

Applying an Expression<> to a LINQ Where clause

我是研究僧i 提交于 2020-07-23 06:38:23
问题 I'm writing a custom Entity Framework filter. I have a list of IDs, and a user-supplied expression. protected IEnumerable<TColumn> FilterIds; protected Expression<Func<T, IEnumerable<TColumn>, bool>> Filter; Now my question is how do I apply the Filter expression to a Where() clause? public virtual IQueryable<T> ApplyFilter(IQueryable<T> query) { if (FilterMode == FilterModeMatchAny && HasFilterIds) { // Whoops! Can't do this! return query.Where(x => Filter(x, FilterIds)); } return query; 回答1

Applying an Expression<> to a LINQ Where clause

淺唱寂寞╮ 提交于 2020-07-23 06:37:34
问题 I'm writing a custom Entity Framework filter. I have a list of IDs, and a user-supplied expression. protected IEnumerable<TColumn> FilterIds; protected Expression<Func<T, IEnumerable<TColumn>, bool>> Filter; Now my question is how do I apply the Filter expression to a Where() clause? public virtual IQueryable<T> ApplyFilter(IQueryable<T> query) { if (FilterMode == FilterModeMatchAny && HasFilterIds) { // Whoops! Can't do this! return query.Where(x => Filter(x, FilterIds)); } return query; 回答1

Applying an Expression<> to a LINQ Where clause

我与影子孤独终老i 提交于 2020-07-23 06:36:12
问题 I'm writing a custom Entity Framework filter. I have a list of IDs, and a user-supplied expression. protected IEnumerable<TColumn> FilterIds; protected Expression<Func<T, IEnumerable<TColumn>, bool>> Filter; Now my question is how do I apply the Filter expression to a Where() clause? public virtual IQueryable<T> ApplyFilter(IQueryable<T> query) { if (FilterMode == FilterModeMatchAny && HasFilterIds) { // Whoops! Can't do this! return query.Where(x => Filter(x, FilterIds)); } return query; 回答1

FromSql with Non-Existing Entity

非 Y 不嫁゛ 提交于 2020-07-22 21:41:11
问题 I have a very large, existing database with numerous views and tables. I need to combine the results of some views and tables. I want this combined data to be available in my DbContext but there is no single corresponding view or table that I can use to map the entity to the SQL results I want. To this end, I have set up a DbQuery in my context and I am using the OnModelCreating method to set up the query results, an example is below. Here are the entity object models. In this example Author

FromSql with Non-Existing Entity

删除回忆录丶 提交于 2020-07-22 21:40:22
问题 I have a very large, existing database with numerous views and tables. I need to combine the results of some views and tables. I want this combined data to be available in my DbContext but there is no single corresponding view or table that I can use to map the entity to the SQL results I want. To this end, I have set up a DbQuery in my context and I am using the OnModelCreating method to set up the query results, an example is below. Here are the entity object models. In this example Author