Entity Framework Core Succinctly Released

My latest (and fifth) ebook for Syncfusion’s Succinctly collection is out: Entity Framework Core Succinctly! It covers Entity Framework Core 2.0 and you can download it for free – need to register first, though.

This book is inspired by my previous one on Entity Framework Code First, but quite a lot has changed.

Huge thanks to Jeff Boenig for the technical review and to Hillary Bowling, Tres Watkins and Jacqueline Bieringer of Syncfusion for all their support.

2017 in Retrospective, 2018 in Perspective

site2017 was a difficult year for me, for personal reasons. So I didn’t blog that much, to be honest.

I was renewed again as Microsoft MVP, an excellent way to start the year! Hooray! Open-mouthed smile

In 2017 my second hardcopy book, Mastering ASP.NET Core 2.0, came out. It was tough, as it is a long book, and it spread from ASP.NET Core 1.1 to 2.0, but I am generally pleased with the result. You can get it in print or ebook format.

I started, with the support from Fusion Cowork and the Aveiro development community, the Fusion/Aveiro Tech Talks here in Aveiro. We’ve had 3 sessions so far, and more are on the list.

Entity Framework Core 2 was released and I talked about it. Also posted about ASP.NET Core 2, but not as much as I would have liked to.

I also covered Postal.NET, my pet project for writing decoupled .NET standard applications.

Started writing content for Stackify, and I got two posts out: .NET Core 2.1 Release: What to Expect in 2018 and How to Call WCF Services and Create SOAP Services with ASP.NET Core. More to come! Winking smile

In early 2017 I made some prophecies about what technologies would be cool that year. Looking back, I can see that I got some of them right, others probably not. I didn’t cover R or WebAssembly, and I was happy I didn’t, as I really didn’t see much going on around them – but I may be wrong, though. I also left out Blockchain, which seems to be the next cool thing. Maybe this is the year for them!

I didn’t write a single post on ASP.NET WebForms or SharePoint; this probably means that they are dead to me. I basically only work with ASP.NET Core or .NET Standard nowadays.

I would like to resume my old classics – NHibernate pitfalls, Entity Framework pitfalls – plus MongoDB in .NET, Visual Studio tips and Java vs C#. All are in the pipeline, plus some C# language improvements and some MediatR, NHibernate and Elasticsearch too! Winking smile

Something that I’ve been working on is the list of Portuguese (meaning, written by portuguese-speaking developers or in portuguese) development sites: https://weblogs.asp.net/ricardoperes/sites-portugueses-sobre-desenvolvimento-de-software. I would really like to keep this up to date! For this to be helpful, however, it should only contain “live” sites, those that have activity. I count on you folks to keep me posted on that! Winking smile

Anyway, thank you all for reading this blog, you’re the reason I write it! Keep dropping by and sending me your comments/questions/corrections/complaints! Smile

  • Entity Framework Core Pitfalls: No TransactionScope Support

    Entity Framework Core, as of version 2.0, does not support enlisting in ambient transactions, like those provided by TransactionScope. It is being tracked by issue #9561. This is by design and is related to a limitation of System.Data.SqlClient, which will be fixed when .NET Core 2.1 comes out. The ticket for SqlClient is #3114.

    I already talked about the hard relation between Entity Framework and TransactionScope before. This time, however, with EF Core 2.1, it seems that it will be fixed, and this is a good thing. For now, however, you will have to roll out your own transaction management strategy which includes starting your own transactions explicitly and either committing them or rolling them back at the end. Please refer to the Microsoft documentation available at https://docs.microsoft.com/en-us/ef/core/saving/transactions.

    Soft Deletes with Entity Framework Core 2 – Part 2

    My previous post explained how to setup soft deletes for queries automatically. This time, I’m going to talk about the other part: actually replacing a delete for an update that sets the is deleted column.

    The key here is to intercept the SaveChanges method, find out all entities that are ISoftDeletable and are marked for deletion and then set their IsDeleted property and change their state to be modified instead of deleted:

    public override int SaveChanges()
    foreach (var entry in this.ChangeTracker.Entries().Where(e => e.State == EntityState.Deleted))
    if (entry.Entity is ISoftDeletable)
    entry.Property(_isDeletedProperty).CurrentValue = true;
    entry.State = EntityState.Modified;

    return base.SaveChanges();
    This way Entity Framework will know what to do and just update them instead of deleting them. Next time, they won’t be loaded as they have been marked as deleted.

    Soft Deletes with Entity Framework Core 2 – Part 1

    Entity Framework Core 2, already covered here, includes a cool feature called global filters. By leveraging global filters, we can apply restrictions automatically to entities, either loaded directly or through a collection reference. If we add this to shadow properties (in the case of relational databases, columns that exist in a table but not on the POCO model), we can do pretty cool stuff.

    In this example, I am going to create a soft delete global filter to all entities in the model that implement a marker interface ISoftDeletable.

    public interface ISoftDeletable
    We just need to override the DbContext’s OnModelCreating method to automatically scan all known entities to see which implement this interface and then create the restriction automatically:
    private const string _isDeletedProperty = "IsDeleted";
    private static readonly MethodInfo _propertyMethod = typeof(EF).GetMethod(nameof(EF.Property), BindingFlags.Static | BindingFlags.Public).MakeGenericMethod(typeof(bool));

    private static LambdaExpression GetIsDeletedRestriction(Type type)
    var parm = Expression.Parameter(type, "it");
    var prop = Expression.Call(_propertyMethod, parm, Expression.Constant(_isDeletedProperty));
    var condition = Expression.MakeBinary(ExpressionType.Equal, prop, Expression.Constant(false));
    var lambda = Expression.Lambda(condition, parm);
    return lambda;

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    foreach (var entity in modelBuilder.Model.GetEntityTypes())
    if (typeof(ISoftDeletable).IsAssignableFrom(entity.ClrType) == true)
    entity.AddProperty(_isDeletedProperty, typeof(bool));


    So, for each entity known from the context we add a shadow property called IsDeleted of type bool. Of course, needless to say, it must also exist on the database. The reason I’m making it a shadow property is to avoid people tampering with the entities, by setting or unsetting its value. This way, the restriction is always performed and it is invisible to us. After we create the property, we add a restriction to the entity’s type.
    Simple, don’t you think? This way, if you want to enable or disable it for a number of entities, just have them implement the ISoftDeletable interface.