entity-framework-core

EF Core linq and conditional include and theninclude problem

亡梦爱人 提交于 2020-05-25 07:42:25
问题 I am having a problem with getting a result when trying to get objects with multiple levels. This is what I am trying to do roughly: _context.Investors.Where(s => s.Id == userId) .Include(c => c.Coins) //only want this if some kind of flag is given by the user. .ThenInclude(ct => ct.CoinType) .Include(c => c.Bricks) //only want this if some kind of flag is given by the user. Essentially I am getting a lot of flags indicating if I should include parts of the object. I got it almost to work.

EF Core linq and conditional include and theninclude problem

混江龙づ霸主 提交于 2020-05-25 07:42:05
问题 I am having a problem with getting a result when trying to get objects with multiple levels. This is what I am trying to do roughly: _context.Investors.Where(s => s.Id == userId) .Include(c => c.Coins) //only want this if some kind of flag is given by the user. .ThenInclude(ct => ct.CoinType) .Include(c => c.Bricks) //only want this if some kind of flag is given by the user. Essentially I am getting a lot of flags indicating if I should include parts of the object. I got it almost to work.

EF Core no .Include() method on DBset

孤街浪徒 提交于 2020-05-25 06:02:32
问题 I'm currently completely unable to call .Include() and intellisense (in vscode) doesn't seem to think it exists. Now after a long time searching the web I've found this: Not finding .Include() method in my EF implementing Generic repository which seems to suggest that .Include exists only in System.Data.Entities, which is only available for EF 5 and 6. So how do i eager load my list property for an entity in EF core? heres my context public class Database : DbContext { //Set new datasources

Updating entity in EF Core application with SQLite gives DbUpdateConcurrencyException

自闭症网瘾萝莉.ら 提交于 2020-05-25 05:05:10
问题 I try to use optimistic concurrency check in EF Core with SQLite. The simplest positive scenario (even without concurrency itself) gives me Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException: 'Database operation expected to affect 1 row(s) but actually affected 0 row(s). Data may have been modified or deleted since entities were loaded . Entity: public class Blog { public Guid Id { get; set; } public string Name { get; set; } public byte[] Timestamp { get; set; } } Context: internal

Entity Framework Core: User Defined SQL Functions

柔情痞子 提交于 2020-05-23 21:10:45
问题 Is it possible to invoke a user-defined SQL function from the query interface in EF Core? For example, the generated SQL would look like select * from X where dbo.fnCheckThis(X.a, X.B) = 1 In my case, this clause is in addition to other Query() method calls so FromSQL() is not an option. 回答1: I just managed this with help from this article (H/T @IvanStoev for his comment on the question). In your DbContext class: [DbFunction("my_user_function_name")] public static bool SatisfiesMyUserFunction

EF can you make a Mutli-column Index using shadow properties?

╄→гoц情女王★ 提交于 2020-05-23 12:24:06
问题 I'm trying to create a multi-column unique index using a shadow property. I know I can solve this problem with just adding a property, but I would like to see if this is possible in a way to keep my model clean. To create a multi-column index you have the following option in Fluent API: modelBuilder.Entity<AlbumTrack>().HasIndex(t => new { t.TrackNumber, t.AlbumId).IsUnique(); But I don't want to clutter my model with an extra AlbumId property and thus would like to use a shadow property, for

.Net Core 3.0 possible object cycle was detected which is not supported

旧时模样 提交于 2020-05-21 04:06:49
问题 I have 2 entities that are related as one to many public class Restaurant { public int RestaurantId {get;set;} public string Name {get;set;} public List<Reservation> Reservations {get;set;} ... } public class Reservation{ public int ReservationId {get;set;} public int RestaurantId {get;set;} public Restaurant Restaurant {get;set;} } If I try to get restaurants with reservations using my api var restaurants = await _dbContext.Restaurants .AsNoTracking() .AsQueryable() .Include(m => m

.Net Core 3.0 possible object cycle was detected which is not supported

孤人 提交于 2020-05-21 04:06:32
问题 I have 2 entities that are related as one to many public class Restaurant { public int RestaurantId {get;set;} public string Name {get;set;} public List<Reservation> Reservations {get;set;} ... } public class Reservation{ public int ReservationId {get;set;} public int RestaurantId {get;set;} public Restaurant Restaurant {get;set;} } If I try to get restaurants with reservations using my api var restaurants = await _dbContext.Restaurants .AsNoTracking() .AsQueryable() .Include(m => m

Entity Framework Core Error: An error occurred using the connection to database '' on server 'localhost'

≯℡__Kan透↙ 提交于 2020-05-17 08:45:07
问题 I have made an ASP.NET Core 3.1 Web-based application and want to use MySQL as the database. I have been following along with some YouTube tutorials on creating MySQL database with ASP.NET Core 3.1 [code first approach] including a tutorial from this site: https://docs.microsoft.com/en-us/aspnet/core/data/ef-rp/intro?view=aspnetcore-3.1&tabs=visual-studio I have created a DataModel Class , added a service to UseMySQL to the Startup.cs Class and created an AppDBContext Class that implements

How to use logical alternative in dynamic query building method

旧时模样 提交于 2020-05-17 05:45:06
问题 So what I am basically trying to do is to build a dynamic sql query. User selects at least two employees through UI and wants to retrieve their schedule. I can't find a solution on how to automatize the query building process without diving into string parsing. Take a look at this code: List<OneEmployee> list = Employees.Where(each => each.Id == 1 || each.Id == 2).ToList(); ... IQueryable<OneDayInSchedule> query = context.Schedule.Where(each => each.property >= 10); foreach (var v in list) {