This is the 27th day of my participation in the August Genwen Challenge.More challenges in August

  • 📢 welcome to like: 👍 collect ⭐ message 📝 if there are mistakes please correct, give people rose, hand left lingering fragrance!
  • 📢 This article was originally written by Webmote and originally published by Nuggets.
  • 📢 author’s motto: life is toss about, when you don’t toss about life, life will start to toss about you, let us come on together! 💪 💪 💪

1. Mapping confusion from RowVersion

RowVersion fields in Sqlserve are usually used as optimistic locks for concurrent writes, but we usually use it as byte[] mapping, no need to assign to it.

But sometimes because of business needs, maybe we need to read it, do some business actions, how do we map it, can we map it directly to long or ULONG?

Byte [] cannot be mapped to any other type.

[] ulONG [] ulong [] ulong [] ulong [] ulong [] ulong [] ulong [] ulong

2. EF value conversion

In fact, EF provides the value conversion function, which can complete various types of read and write conversion.

A converter allows you to convert attribute values when reading or writing to the database. This conversion can convert one value to another of the same type, and you don’t need to deal with null because null data doesn’t call the converter.

3. Configure the converter

Value in the DbContext. OnModelCreating configured.

protected override void OnModelCreating(ModelBuilder modelBuilder) {
        modelBuilder 
        .Entity<Rider>()
        .Property(e => e.Mount)
        .HasConversion( 
        v => v.ToString(),
        v => (EquineBeast)Enum.Parse(typeof(EquineBeast), v)
        ); 
}
Copy the code

HasConversion provides 2 funCs for conversion from CLR to DB and from DB to CLR.

EF has so many commonly used transformations built in that you almost never have to rewrite them.

Such as:

modelBuilder .Entity<Rider>() .Property(e => e.Mount) .HasConversion<string>();
Copy the code

Of course, it’s easier to just use the attribute tag Column

public class Rider2 { public int Id { get; set; } [Column(TypeName = "nvarchar(24)")] public EquineBeast Mount { get; set; }}Copy the code

or

modelBuilder .Entity<Rider2>() 
.Property(e => e.Mount) 
.HasColumnType("nvarchar(24)");
Copy the code

There are many built-in conversions:

  • .HasConversion()
  • .HasConversion()
  • .HasConversion()

.

4. RowVersion conversion

SQL Server uses 8[byte binary columns][support optimistic RowVersion/TIMESTAMP concurrency], which always use 8-byte arrays to read and write from the database.

However, byte arrays are mutable reference types, which makes them a bit tricky to work with.

Value converters allow it to be mapped to types such as long.

For example, suppose the Blog entity has ulONG and issues a token:

public class Blog { public int Id { get; set; } public string Name { get; set; } public ulong Version { get; set; }}Copy the code

SQL RowVersion server columns can be placed using a value converter:

modelBuilder.Entity<Blog>()
    .Property(e => e.Version)
    .IsRowVersion()
    .HasConversion<byte[]>();
Copy the code

5. Encryption attribute value

Value converters can also be used to encrypt attribute values before sending them to the database, and then decrypt them when they are read.

For example, using string inversion instead of the actual encryption algorithm:

modelBuilder.Entity<User>().Property(e => e.Password).HasConversion(
    v => new string(v.Reverse().ToArray()),
    v => new string(v.Reverse().ToArray()));
Copy the code

6. Summary

Value conversions make it much easier to manipulate special types in the database, not just RowVersion types, but jSON-formatted strings, dates, and other special types that can benefit from value conversions.

Routine summary, rational view!

Knot is what ah, knot is I want you to praise but can not get lonely. 😳 😳 😳

👓 have seen this, but also care about a thumbs-up?

👓 has been liked, but also care about a collection?

👓 are collected, but also care about a comment?