Entity Framework 6 - optimistic concurrency
Concurrency conflicts occur when more than one user wants to edit the same entity stored in the database at the same time. Let’s say that we have two users Mark and John currently using our application and they want to edit the same entity stored in DB which Id is equal to 2 (please refer to image below).
C# 6 feature - using static directive
In C# 6 was introduced using static
directive. By using this language construct you can reference to static class members without specifying a type name.
WPF Validation - Using INotifyDataErrorInfo
In the .NET 4.5 was introduced new interface INotifyDataErrorInfo
which enables data entity classes to implement custom validation rules and expose validation results asynchronously. This interface has three members:
HasErrors
property indicates whether there are any validation errorsGetErrors
method returns anIEnumerable
that contains validation errors for the specified property (when the propertyName parameter isn’t equal to null or empty string) or for the entire entity (when the propertyName parameter is equal to null or empty string)ErrorsChanged
event must occur when the validation errors have changed for a property or for the entity
WPF Validation - Using IDataErrorInfo
In this blog post, I will show you how you can validate data using IDataErrorInfo
. This interface has only two members which need to be implemented:
public string Error { get; }
- gets an error message indicating what is wrong with this objectpublic string this[string columnName] { get; }
- get the error message for the property with the given name.
Entity Framework 6 - speed up reading the data
If you want to read data from the database without need of editing it later you can improve your query by using the AsNoTracking
method. This “read-only query” is quicker to execute because EF will no need to set up change tracking information about every single loaded entity.
WPF Validation - Display errors to the user
In this post, I will show how you can present user input validation errors to the user. By default, WPF shows a red border around the TextBox
when the entered value is invalid. But in this case, our user has no idea what is wrong with entered data. We need to inform the user by providing an error message on the view.
WPF Validation - Using ValidatesOnExceptions
In WPF we have few options to validate user input data. In this article, I will show you validation based on the exception. This validation is applied in very simple solutions. The only thing you should do is throw an exception on property level and set ValidatesOnExceptions
to true in the view.
Entity Framework 6 Code First - add constrains
EF6 has two ways to where you can add constraints to your model. One of them is Fluent API and the other one is Data Annotations.
Entity Framework 6 Code First - singular table names
By default, EF 6 Code First creates a table in your database with the convention that the table name to be a pluralized version of the entity type name.
Json.NET - ignoring properties during serialization
When you’re using Json.NET to serialize an object by default all public fields and properties will be serialized. In this case, if you want to ignore some of them you can use JsonIgnore
attribute. In the following example, you can see how you can ignore SSN
property during the serialization process.