concept default in category entity framework

appears as: default, default, defaults, The default
Entity Framework Core in Action, SE

This is an excerpt from Manning's book Entity Framework Core in Action, SE.

The first issue is object-relational impedance mismatch. Database servers and object-oriented software use different principles: databases use primary keys to define that a row is unique, whereas .NET class instances are, by default, considered unique by their reference. EF Core handles most of this for you, but your .NET classes gain primary and foreign keys, which is extra data only needed for the database. Your software-only version of the classes don’t need those extra properties, but the database does.

In EF6.x, when you call SaveChanges, EF6.x by default will validate the data by using the standard .NET validation approach: EF6.x looks for data validation attributes and, if present, runs IValidatableObject.Validate on entity classes. EF Core doesn’t include this feature because a lot of validation is done in the frontend, but it’s not hard to add a validation feature if you need it. Chapter 4 shows you how to do this.

Entity Framework Core in Action

This is an excerpt from Manning's book Entity Framework Core in Action.

Part 2 covers how and why you can change the defaults, and looks deeper into some of the EF Core commands. After part 2, you’ll be able to use EF Core to create a database in exactly the way you want it, or link to an existing database that has a specific schema, or design. In addition, by using some of EF Core’s advanced features, you can change the way the database data is exposed inside your .NET application—for instance, controlling software access to data more carefully or building code to automatically track database changes.

When you read in an entity, it’s tracked by EF Core by default. This is known as a tracked entity, and EF Core holds extra information on the entity.

Listing 6.3 BookConfig extension class configures Book entity class
internal class BookConfig : IEntityTypeConfiguration<Book>
{
    public void Configure
        (EntityTypeBuilder<Book> entity)
    {
        entity.Property(p => p.PublishedOn)  #1  
            .HasColumnType("date");        
        entity.Property(p => p.Price)    #2  
            .HasColumnType("decimal(9,2)");
        entity.Property(x => x.ImageUrl)    #3  
            .IsUnicode(false);
        entity.HasIndex(x => x.PublishedOn);    #4  
        //Model-level query filter

        entity
            .HasQueryFilter(p => !p.SoftDeleted);    #5  
    }
}

#1   Convention-based mapping for .NET DateTime is SQL datetime2. This command changes the SQL column type to date, which holds only the date, not the time.
#2   Sets a smaller precision and scale of (9,2) for the price instead of the default (18,2)
#3   The convention-based mapping for .NET string is SQL nvarchar (16 bit Unicode). This command changes the SQL column type to varchar (8-bit ASCII).
#4   Adds an index to the PublishedOn property because you sort and filter on this property
#5   Sets a model-level query filter on the Book entity. By default, a query will exclude Book entities when the SoftDeleted property is true.

Figure 8.3 Two pieces of code (say, on a web application) that are running in parallel and make a near-simultaneous update of the same column (in this case, the publication date of the same book). By default, EF Core allows the second write to win, and the first write is lost. This is called optimistic concurrency, but the “last write win” rule may not be useful in all cases.

c08_03.png