entity-framework-core

ASP.NET Core DbContext injection

会有一股神秘感。 提交于 2020-06-25 09:02:28
问题 I have a ConfigurationDbContext that I am trying to use. It has multiple parameters, DbContextOptions and ConfigurationStoreOptions . How can I add this DbContext to my services in ASP.NET Core? I have attempted the following in my Startup.cs: ConfigureServices .... services.AddDbContext<ConfigurationDbContext>(BuildDbContext(connString)); .... private ConfigurationDbContext BuildDbContext(string connString) { var builder = new DbContextOptionsBuilder<ConfigurationDbContext>(); builder

How do I solve SqlNullValueException?

你离开我真会死。 提交于 2020-06-23 07:29:16
问题 I'm trying to find an entity but I get a SqlNullValueException when entity framework is trying to get the value of a field that's Null. I checked in the database and all the not nullable fields are filled. The only fields that have a NULL value are the ones that are allowed to have it. In other answers I found that I should remove required attribute from the class properties or remove the IsRequired method from modelbuilder definition, however I autogenerated these and they never had a

EF Core 3, optimize lots of Include/ThenInclude

时光怂恿深爱的人放手 提交于 2020-06-23 01:52:48
问题 I have a query like this return await _ctx.Activities .Include(a => a.Attributes) .Include(a => a.Roles) .Include(a => a.Bookmarks) .Include(a => a.VideoMetas) .ThenInclude(vm => vm.Instances) .Include(a => a.ImageMetas) .ThenInclude(im => im.Instances) .Include(a => a.Procedure) .ThenInclude(p => p.Attributes) .FirstOrDefaultAsync(a => a.Id == id); Which turns out to be very slow. In EF 6 you can do .Include(v => v.VideoMetas.Select(vm => vm.Instances) which is a bit faster (I guess, haven't

ThenInclude not recognized in EF Core query

两盒软妹~` 提交于 2020-06-22 17:53:45
问题 My IQueryable looks like this: IQueryable<TEntity> query = context.Set<TEntity>(); query = query.Include("Car").ThenInclude("Model"); 'IQueryable' does not contain a definition for 'ThenInclude' and no extension method 'ThenInclude' accepting a first argument of type 'IQueryable' could be found (are you missing a using directive or an assembly reference?) I have all references needed: using Content.Data.Models; using Microsoft.EntityFrameworkCore; using System; using System.Collections

How can I configure Entity Framework to automatically trim values?

早过忘川 提交于 2020-06-22 04:26:29
问题 I need to make “EF Core 2.1.0” remove white space from Strings fields in queries, “HasConversion” not is not working, can you tell me why? entity.Property(e => e.Name) .HasConversion( new ValueConverter<string, string>(v => v.TrimEnd(), v => v.TrimEnd())); -using DB2 database and .net core 2.1 Query: public List<ItemServico> List() { return _uow._db.ItensServico.ToList(); } 回答1: That's what the varchar type is for, to trim spaces automatically, and efficiently. Manual trim() operations have

How can I configure Entity Framework to automatically trim values?

自古美人都是妖i 提交于 2020-06-22 04:25:08
问题 I need to make “EF Core 2.1.0” remove white space from Strings fields in queries, “HasConversion” not is not working, can you tell me why? entity.Property(e => e.Name) .HasConversion( new ValueConverter<string, string>(v => v.TrimEnd(), v => v.TrimEnd())); -using DB2 database and .net core 2.1 Query: public List<ItemServico> List() { return _uow._db.ItensServico.ToList(); } 回答1: That's what the varchar type is for, to trim spaces automatically, and efficiently. Manual trim() operations have

Does EF Core have a connection pool?

跟風遠走 提交于 2020-06-17 02:36:07
问题 The DbContext of EF Core is scoped in the ASP.NET Core services. So a new DbContext is created on each request. Does this mean that a new connection is opened on each request, including all the overhead like logging in to the database? Or is there a connection pool that is independent of the DbContext ? 回答1: The underlying ADO.NET provider like System.Data.SqlClient usually implements a connection pool 来源: https://stackoverflow.com/questions/56635538/does-ef-core-have-a-connection-pool

Calling a simple stored procedure in EF core 3.1

被刻印的时光 ゝ 提交于 2020-06-17 01:41:29
问题 I have a very simple stored procedure: CREATE PROCEDURE [dbo].[ClearIterations] AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON delete from iterations END GO When calling it from EF it is not called. I get no errors: public void ClearIterations() { this.Iterations.FromSqlRaw("ClearIterations").IgnoreQueryFilters(); } Any pointers? I found the sample above on another thread in here that where the code above is the answer.

Calling a simple stored procedure in EF core 3.1

那年仲夏 提交于 2020-06-17 01:41:11
问题 I have a very simple stored procedure: CREATE PROCEDURE [dbo].[ClearIterations] AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON delete from iterations END GO When calling it from EF it is not called. I get no errors: public void ClearIterations() { this.Iterations.FromSqlRaw("ClearIterations").IgnoreQueryFilters(); } Any pointers? I found the sample above on another thread in here that where the code above is the answer.

Entity Framework Core PostgreSQL json query

牧云@^-^@ 提交于 2020-06-13 09:07:12
问题 Always return empty results var collection = await _context.Settings .Select(s => new { s.SettingId, s.SettingParentId, SettingValue = s.SettingValue.GetProperty(lang) }) .Where(s => EF.Functions.JsonExists(s.SettingValue, lang)) .ToListAsync(); I try return only key from json but always return empty, when remove "select" works fine but i need only one key This is the model public class Setting { public string SettingId { get; set; } public string SettingParentId { get; set; } public