entity-framework-core

Unexpected behaviour with Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ForEachAsync<T>()

让人想犯罪 __ 提交于 2021-02-07 14:44:50
问题 Here are the steps to reproduce. The below program copies 10,000 rows from one SQL table to another using .Net Core console app and EF Core. The program inserts records in 100 batches, and (this is important!) it creates a new instance of DbContext for each insert. 1) Create SQL Server database, and the "Froms" and "Tos" tables: create table Froms ( Id int identity(1, 1) not null, Guid [uniqueidentifier] not null, constraint [PK_Froms] primary key clustered (Id asc) ) go create table Tos ( Id

EF Core - how to audit trail with value objects

孤人 提交于 2021-02-07 13:14:51
问题 I'm trying to implement an Audit Trail (track what changed, when and by whom) on a selection of classes in Entity Framework Core. My current implementation relies on overriding OnSaveChangesAsync: public override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default) { var currentUserFullName = _userService.CurrentUserFullName!; foreach (var entry in ChangeTracker.Entries<AuditableEntity>()) { switch (entry.State) { case EntityState.Added:

EF Core - how to audit trail with value objects

左心房为你撑大大i 提交于 2021-02-07 13:14:02
问题 I'm trying to implement an Audit Trail (track what changed, when and by whom) on a selection of classes in Entity Framework Core. My current implementation relies on overriding OnSaveChangesAsync: public override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default) { var currentUserFullName = _userService.CurrentUserFullName!; foreach (var entry in ChangeTracker.Entries<AuditableEntity>()) { switch (entry.State) { case EntityState.Added:

One Entity 2 Tables in EF Core 2.0

不打扰是莪最后的温柔 提交于 2021-02-07 12:33:14
问题 With EF Core 2.0 is possible to map one entity in 2 tables? Something similar to this in EF6 (the 2 configurations are equal, they are just samples). protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.ApplyConfiguration(delegate(EntityMappingConfiguration<Student> studentConfig) { studentConfig.Properties(p => new { p.Id, p.StudentName }); studentConfig.ToTable("StudentInfo"); }); Action<EntityMappingConfiguration<Student>> studentMapping = m => { m.Properties(p

Detect lazy-load in Entity Framework Core

六月ゝ 毕业季﹏ 提交于 2021-02-07 10:01:58
问题 Entity Framework Core 3.1.2 - I have enabled UseLazyLoadingProxies on my DbContext to ensure data integrity, but I want to throw an exception during development if it is used. How can I execute some code every time EF Core loads a relationship lazily? 回答1: The only way I know is diagnostic messages. See an example here: https://www.domstamand.com/getting-feedback-from-entityframework-core-through-diagnostics. The event class you want is https://docs.microsoft.com/en-us/dotnet/api/microsoft

Defining Self Referencing Foreign-Key-Relationship Using Entity Framework 7 Code First

Deadly 提交于 2021-02-07 09:00:20
问题 I have an ArticleComment entity as you can see below: public class ArticleComment { public int ArticleCommentId { get; set; } public int? ArticleCommentParentId { get; set; } //[ForeignKey("ArticleCommentParentId")] public virtual ArticleComment Comment { get; set; } public DateTime ArticleDateCreated { get; set; } public string ArticleCommentName { get; set; } public string ArticleCommentEmail { get; set; } public string ArticleCommentWebSite { get; set; } public string AricleCommentBody {

Asp.net core Identity “The INSERT statement conflicted with the FOREIGN KEY constraint ”

混江龙づ霸主 提交于 2021-02-07 07:51:41
问题 I create ASP.NET CORE application with ASP.NET CORE Identity. I create seed class for saving new users and roles for first startup application. Inside this seed class I get following error when I add Role To User. The INSERT statement conflicted with the FOREIGN KEY constraint "FK_AspNetUserRoles_AspNetUsers_UserId". The conflict occurred in database "DB_A14695_Elvinm", table "dbo.AspNetUsers", column 'Id'. The statement has been terminated. I used following class for Identity public class

Select only specific fields with Linq (EF core)

霸气de小男生 提交于 2021-02-07 07:21:31
问题 I have a DbContext where I would like to run a query to return only specific columns, to avoid fetching all the data. The problem is that I would like to specify the column names with a set of strings, and I would like to obtain an IQueryable of the original type, i.e. without constructing an anonymous type. Here is an example: // Install-Package Microsoft.AspNetCore.All // Install-Package Microsoft.EntityFrameworkCore using Microsoft.EntityFrameworkCore; using System; using System.Linq;

Entity Framework 7 SaveChanges

寵の児 提交于 2021-02-07 06:57:21
问题 Is there any way to register a callback that will be called before a model in EF7 is saved to the database? What I want to do is to set a ModifiedBy and ModifiedDate property that I have on all models. I'm not that keen to do this manually before each save so I'm looking for some more generic and automatic way. 回答1: ChangeTracker.Entries() allows you to get all of the entity changes. You could override SaveChanges in your DbContext and set the modified properties using something like the

How to test database views using Entity Framework Core's in memory DB provider?

放肆的年华 提交于 2021-02-07 05:30:11
问题 When I tried to add objects to views, it throws exception saying unable to track an instance of type because it is a query type . Is there a way to get around this? 回答1: Query Types are read-only by definition (for all database providers, not only for in memory): Are never tracked for changes on the DbContext and therefore are never inserted, updated or deleted on the database. However, additionally to their usual usage scenarios of Mapping to database views. Mapping to tables that do not