Efcore operation entity attribute and implementation model of DDD

Time:2022-4-19

1. Efcore operation entity attribute and implementation model of DDD

1.1 efcore operation entity attribute

(1) Object operation entity properties will call get and set methods

image-20220318211612469

(2) Based on the consideration of performance and support for special functions, when reading and writing attributes, EF core will directly skip get and set and directly operate the member variables that actually store attribute values if possible

(3) When reading and writing the attribute of an entity object, EF core will find the member variable corresponding to the attribute. If it can be found, EF core will directly read and write the value of this member variable instead of reading and writing through set and get code blocks.

(4) EF core will try to directly read and write the member variable corresponding to the attribute according to the naming rules. Only when the corresponding member variable cannot be found according to the naming rules, EF core will read and write the attribute value through the get and set code blocks of the attribute. The default behavior can be modified through the usepropertyaccessmode () method in the fluent API.

1.2 efcore implementation model

User

public class User
{
    //Properties are read-only or can only be modified by internal code.
    //Define the set of attributes as private or init, and then assign initial values to these attributes through the construction method.
    public int Id { get; init; }

    public DateTime CreatedDateTime { get; init; }

    public string UserName { get; private set; }

    public int Credit { get; private set; }

    //Map private member variables to columns in the data table.
    //builder. Property ("member variable name")
    private string? passwordHash;

    //Some attributes are read-only, that is, their values are read from the database, but we cannot modify the attribute values.
    //In the code for configuring the entity class, use hasfield ("member variable name") to configure the properties
    private string? remark;
    public string? Remark 
    {
        get { return remark; }
    }

    //Some attributes do not need to be mapped to data columns and are only used at run time.
    //Ignore this property.
    public string? Tag { get; set; }



    //The name of the parameter in the creation method is consistent with the name of the attribute.

    public User(string userName, DateTime createdDateTime, int credit)
    {
        this.UserName = userName;
        this.CreatedDateTime = createdDateTime;
        this.Credit = credit;
    }

    //The parameterless construction method is defined as private.
    //Efcore usage
    /*
    private User() { }


    //If the constructor and property names do not match
    public User(string un, DateTime time, int c)
    {
        this.UserName = un;
        this.CreatedDateTime = time;
        this.Credit = c;
    }
    */

}

UserConfig

public class UserConfig : IEntityTypeConfiguration
{
    public void Configure(EntityTypeBuilder builder)
    {
        builder.ToTable("T_Users");
        builder.Property(u => u.UserName).IsRequired().HasMaxLength(50);
        //builder. Property maps private member variables to columns in the data table.
        builder.Property("passwordHash").IsRequired().HasMaxLength(100);
        //The attribute is read-only, that is, its value is read from the database, but we cannot modify the attribute value.
        //Use hasfield ("member variable name") to configure properties
        builder.Property(u => u.Remark).HasField("remark").HasMaxLength(100); ;
        //builder. Some attributes of ignore do not need to be mapped to data columns and are only used at run time.
        builder. Ignore(u => u.Tag);// Feature five
    }
}

(1) Properties are read-only or can only be modified by internal code.

Implementation: define the set of attributes as private or init, and then assign initial values to these attributes through the construction method.

image-20220319144529869

(2) Define a construction method with parameters.

realization:

  • The parameterless construction method is defined as private.

image-20220319144554151

  • No parameter free construction method is defined in the entity class, only meaningful parameter construction methods are defined, but the name of the parameter in the construction method is required to be consistent with the name of the attribute.

image-20220319144705469

(3) Some member variables have no corresponding attributes, but these member variables need to be mapped to columns in the data table, that is, we need to map private member variables to columns in the data table.

realization:

builder. Property (“member variable name”)

image-20220319144724926

(4) Some attributes are read-only, that is, their values are read from the database, but we cannot modify the attribute values.

In the code for configuring the entity class, use hasfield (“member variable name”) to configure the properties.

image-20220319144746142

(5) Some attributes do not need to be mapped to data columns and are only used at run time.

Use ignore () to configure ignore this property.

image-20220319144916879

(6) View database

Execute the commands add mirgration and update database

image-20220319145027362

Recommended Today

[NOIP2001 Popularization Group] Seek Ranking

Question analysis: The question mentions the pre-order, in-order, and post-order arrangement of trees, so we need to know what these three arrangements mean first. 3 (depth-first) permutations of binary trees: Pre-order, "root left and right". That is, for each subtree of the binary tree, first visit its root, and then traverse its left and right […]