ef-core-2.2

EF Core “Group By could not be translated and will be evaluated locally.”

别等时光非礼了梦想. 提交于 2020-01-23 03:37:08
问题 I am running a simple query against an Sql Server database using Entity Framework Core 2.2.6 however the GroupBy is not being executed on the server, instead it is being executed locally. Is there something i'm missing that will force the group by onto the server? The 2 variations of EF query i have tried: public class Holiday { public int Id {get;set;} public DateTime Date {get;set;} public string Username {get;set;} public string Approver {get;set;} } //version 1 await _dbContext.Holidays

Aggregate of aggregate with EF Core Linq2Sql

雨燕双飞 提交于 2020-01-21 19:18:28
问题 I have a ASP.NET Core 2.2 project with EF Core 2.2 Code-First DB. I have the following entities: Building, which is basically an address with some other important data. Floor, which contains the number of the floor. A building can have multiple floors. A floor has to have exactly one building where it's located. Room, which has a number. A floor can have multiple rooms. A room has to have exactly one floor. WorkGroup, which contains how many employees are int the group, whether the group is

Stop EF Core from using Merge to Insert

五迷三道 提交于 2020-01-05 07:16:43
问题 I am converting my application to .NET Core. In doing so, I am running into issues with EF Core and inserts. If I insert 1 or 2 rows then EF Core performs a normal SQL INSERT statement. But when I have 3 or more rows, it switches to a MERGE statement, which then fails with: The column reference "inserted.MyKeyColumn" is not allowed because it refers to a base table that is not being modified in this statement. My guess is that this is due to the fact that the query is actually running on a

Stop EF Core from using Merge to Insert

左心房为你撑大大i 提交于 2020-01-05 07:16:26
问题 I am converting my application to .NET Core. In doing so, I am running into issues with EF Core and inserts. If I insert 1 or 2 rows then EF Core performs a normal SQL INSERT statement. But when I have 3 or more rows, it switches to a MERGE statement, which then fails with: The column reference "inserted.MyKeyColumn" is not allowed because it refers to a base table that is not being modified in this statement. My guess is that this is due to the fact that the query is actually running on a

Saving entity with owned property

十年热恋 提交于 2019-12-25 01:38:04
问题 Ok, this might be a dumb question, but I can't figure it out (tried searching but with no reasonable effect). I have a model: public class Freelancer { public Address Address { get; set; } public Guid Id { get; set; } } public class Address { public string Name1 { get; set; } public string Name2 { get; set; } public string ZipCode { get; set; } public string City { get; set; } } which is configured as follows: modelBuilder.Entity<Freelancer>().HasKey(x => x.Id); modelBuilder.Entity<Freelancer

using Max() in Orderby

天涯浪子 提交于 2019-12-24 16:45:59
问题 I have this line which runs till I enabled RelationalEventId.QueryClientEvaluationWarning. Here let's say what I want to do is to sort the results (customers) based on their latest order date. .OrderByDescending(x=>x.orders.Max(y=>y.CreateDate)) After I configure the context as follow I realised the Max() is not converted to TSql. protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { base.OnConfiguring(optionsBuilder); optionsBuilder.ConfigureWarnings(warning => {

EF Core - Unique constraint including navigation property not detected in migration

六眼飞鱼酱① 提交于 2019-12-24 12:05:20
问题 I am trying to apply a unique composite constraint, one of the parts of the constraint is a foreign key. The only way I can seem to make it work is to explicitly defined the foreign key in my domain class, which I want to avoid. Is this possible? The problem and the workaround applies to both HasAlternateKey and HasIndex . The solution builds fine, but the constraint is ignored when creating a migration until the shadow property is turned into a real property in the domain class. This does

Using EF Core to invoke stored procedure and closing connection

依然范特西╮ 提交于 2019-12-24 07:39:09
问题 I have an ASP.NET Core 2.2 application using EF Core. I have service class which typically use a DbContext for any CRUD operations. However in one of the method ( Assign method below) I need to use stored procedure. So I am using the following code. Note that DbContext is injected as Scoped instance. public class MyService : IMyService { private readonly MyDbContext _dbContext; public MyService(MyDbContext dbContext) { _dbContext = dbContext; } public async Task<Employee> GetByID(int id) {

EF Core 2.2.3 child record is not saved after update from 2.2.1

岁酱吖の 提交于 2019-12-23 03:45:06
问题 I had the below .net Core code working with Microsoft.EntityFrameworkCore.SqlServer 2.2.1. After I upgrade the dotnet core sdk to 2.2.3 it is not working. Note that the related entity is not mapped with a FK in DB. I have a partial class that has the relation and has the attribute NotMapped to make this work in 2.2.1 . I am not sure what changed and what I need to add to save header and detail record. var soHeader = new masSoHeader(); var orderToAdd = Mapper.Map(salesOrder.SalesOrderHeader,

EF Core Connection to Azure SQL with Managed Identity

房东的猫 提交于 2019-12-20 17:34:53
问题 I am using EF Core to connect to a Azure SQL Database deployed to Azure App Services. I am using an access token (obtained via the Managed Identities) to connect to Azure SQL database. Here is how I am doing that: Startup.cs: public void ConfigureServices(IServiceCollection services) { //code ignored for simplicity services.AddDbContext<MyCustomDBContext>(); services.AddTransient<IDBAuthTokenService, AzureSqlAuthTokenService>(); } MyCustomDBContext.cs public partial class MyCustomDBContext :