Skip to toolbar

Page 2 of 5

Auditing rows in .NET Entity Framework

Many times our projects require to keep some kind of auditing on database rows for changes and updates, so we can know who and when created a row or did the last update to it.

When we have lots of entities and mappings going up and down through our application layers, sometimes is difficult to know where and how we have to handle this task.

In this article, I’ll show you a good technique to implement database row auditing using Entity Framework. Maybe you’ll find another approaches you might consider better, but this is a clean and single method one, and it’s the one I use in my own projects at work.

For the demonstration, I’m gonna use as base the project I created for my previous article “Entity Framework 6.0 code first & TPT, persistence of polymorphic collections“. (You did read it, didn’t you?).

Clone the repository:
Download Zip file:

Continue reading

!Any() vs All()

The dilema

Sometimes you need to do some action to check if an element is not present in a collection, for example, check if a customer is in the Customers collection before trying to add it.

With .NET there are several ways to do so: For example if the collection is a List you can use Exists() or if you want to check the presence of the object itself, you can use Contains(). Using Linq, we can also do it with Any and All

In today’s article, we’re gonna find if it’s better to use !.Any() or .All()

Continue reading

Entity Framework 6.0 code first & TPT, persistence of polymorphic collections

Many times the data structure we need in our applications isn’t as simple as the ideal examples found at most manuals. Sometimes we need more advanced mechanics for the persistence of our data entities.

In today’s article, we’re going to create an entity structure containing a collection of polymorphic elements, in other words, a collection that can contain several object types, all of them inheriting from a common ancestor class.

The problem with polymorphic collections is that, even though their components share a common class, they don’t share all the same properties implemented, so they can’t be saved in a single table at the database. They need a different table for every different final entity.

This is a very common problem but it’s a little difficult to explain. For this, I’ve created a little example project in a “real world” context to try to explain it the best I can, using Entity Framework 6.0 Code First with POCO entities and using the TPT (Table Per Type) feature of EF 6.0+ that simplifies a lot the scenario.

This article includes a link to my Git repository, where you can find a full example solution that will allow you to see the result by yourself, and get your hands dirty making new tests.

Download the solution in .ZIP format
Clone the GIT repository

Continue reading

Using custom attributes to create a entity mapper

.NET custom attributes technique allows us to associate metadata and information to our code entities, be it classes, properties, methods… When we associate an attribute to a code entity, we can later read it again using a technique called Reflection

Reflection itself could take several articles to cover, but I’ll try to show and explain the basic topics of it to be able to use attributes without being a guru in reflection.

For this article I’ve setup a fully functional console project in C# that implements all the necessary stuff to map values from one class to another without having to assign every property by hand…

If you’re the download and play style, you can start by downloading the project from my Bitbucket repository using this links:

Git repository:

Zip download:

But I recommend you to keep reading for a better explanation of what’s happening there! So, without further introduction… let’s get to business!

Continue reading

C# 6.0 New features compilation

As most of you know, or some may not, the new 6.0 versión of C# is baking, and will be out shortly. This new version codenamed “Roslyn” is part of the new Microsoft’s “Opennes” change, and so, can be found as open source at Github.

I’ve been reading articles about the new features it’s gonna bring for some time now from official sites and from people testing the Visual Studio 2015 Preview. I link to the sources at the end of this article.

Some articles point to their favorite feature/s, while some other other try to get a global view. In this article, I’ll try myself to make a compilation of all (or most) of these new features, and, as part of my contribution, will also try to give some “real life” uses of them, so you can judge for yourself if they will or won’t be useful for you.
Continue reading

© 2020 My Coding Corner

Theme by Anders NorénUp ↑

%d bloggers like this: