.NET Core 2 – EF Core Error handling Save changes

Looking through the GitHub issues, there is no DbEntityValidationException equivalent in Entity Framework Core. There’s a blog post (linked from issue #9662 on GitHub), that gives a code example for performing the validation logic yourself, included here for completeness: class MyContext : DbContext { public override int SaveChanges() { var entities = from e in … Read more

Where is the EDMX

There is no edmx support in Entity Framework Core. It only supports a code-first approach. The option to add a new Entity Data Model will be added, but it will produce entity class files instead of an edmx file. The tooling development is a little bit behind the framework development currently.

Unable to edit db entries using EFCore, EntityState.Modified: “Database operation expected to affect 1 row(s) but actually affected 0 row(s).”

The reason is clearly stated in the exception. Check the Id on the role object as you receive it on your Edit action and try to lookup that id in the database. The exception message you see states that, it is expecting to find a row with a matching Id of the object you attached, … Read more

EF Core ‘another instance is already being tracked’

By default when you retrieve entities they are tracked and since they are tracked you could just call SaveChanges and not call Update. You can also retrieve entities without tracking them by using .AsNoTracking() calling Update is needed if not tracked already, so if you use AsNoTracking then you do need to use Update before … Read more

Date Only cannot be mapped SQL Server 2019

Try this public class YourDbContext : DbContext { Ctors() {…} protected override void ConfigureConventions(ModelConfigurationBuilder builder) { builder.Properties<DateOnly>() .HasConversion<DateOnlyConverter>() .HasColumnType(“date”); } } /// <summary> /// Converts <see cref=”DateOnly” /> to <see cref=”DateTime”/> and vice versa. /// </summary> public class DateOnlyConverter : ValueConverter<DateOnly, DateTime> { /// <summary> /// Creates a new instance of this converter. /// </summary> … Read more

Method not found: ‘Void CoreTypeMappingParameters..ctor(System.Type, Microsoft.EntityFrameworkCore.Storage.ValueConversion error

I think this is the same problem that I was seeing, and the solution is described in this question: Error while add migration to create database in Entity Framework 6, method not found CoreTypeMappingParameters In a nutshell, what worked was making sure all the entity framework packages were the same version (in my case 6.0.10). … Read more

How to get user information in DbContext using Net Core

I implemented an approach similar to this that is covered in this blog post and basically involves creating a service that will use dependency injection to inject the HttpContext (and underlying user information) into a particular context, or however you would prefer to use it. A very basic implementation might look something like this: public … Read more