Entity Framewor simple property mapping

In this section we only introduce more common in EF map

Zero, the mapping table name

By default, you can not configure the table name, our name will be used as the model name of the database table. But most of the projects will require specification database table names, for example, we want to model User mapping database for the Users , then we can do so in the context of the derived class OnModelCreating the following definition:

modelBuilder.Entity<User>().ToTbale("Users");
复制代码

A primary key mapping

Table's primary key. We are accustomed to use Id or in Id way to the end of the name, EF will default case Id or Id attribute ending as the primary key, if both are present, the default will be to Id as the primary key. However, there are the following cases: 0. Set the primary key;

  1. The primary key for the int type, but not self-growth, but manually assigned.

For the above two cases, we are as follows:

//设置联合主键
modelBuilder.Entity<User>().HasKey(k => new
{
    Id=k.Id,
    UserId=k.UserId
});

//手动分配主键值
modelBuilder.Entity<User>().HasKey(k => k.Id).Property(p => p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
复制代码

DatabaseGeneratedOption is an enumerated type, the following values:

value Explanation
Identity Identity column
Computed Computed column
None Manually assign value

Second, the value of mapping

There are numeric types in the database are many, C # also has a lot of numeric types, but we can not directly convert numeric types in C # numeric type database. So how would C # value type is mapped to a database numeric type it? Here we # C float , for example, look at the code:

modelBuilder.Entity<User>().Property(p=>p.Float);
复制代码

Through the above code, we will # C float type mapping database for real type. The following table is a value of type C # database values corresponding to the type:

C # numeric types Database numeric types
int int
double float
float real
decimal decimal(18,2)
Int64 bigint

We see that the table has a numeric type C # decimal value corresponding to the type of database is the decimal (18,2) , Reserved 2 bits after the decimal point in parentheses represents, but in some cases we need to retain N bits after the decimal point, which we can do this:

modelBuilder.Entity<User>().Property(p=>p.Money).HasPrecision(18,4);
复制代码

Third, string map

When we are not configured to map a property of type string, the default database type is nvarchar (max) , but in most cases will not use the default mapping. A few examples to explain how to change the default mapping.

  1. Field is not empty
//设置Name属性在数据库映射不可为空
modelBuilder.Entity<User>().Property(p=>p.Name).IsRequired();
复制代码
  1. Field may be empty
//设置Birthday属性在数据库映射可为空
modelBuilder.Entity<User>().Property(p=>p.Birthday).IsOptional();
复制代码

Fourth, date maps

EF type in the date in the database as the default mapping Date, type the date in the database but there are many, and sometimes we need to date other type mapping database type, then how do we do it? Here we mapped DateTime an example:

modelBuilder.Entity<User>().Property(p=>p.CreateDateTime).HasColumnType("DATETIME");
复制代码

Note: the date type is numeric types and value type, so we do not by IsRequired to configure the mapping field is not empty, because the default is not empty. However, by IsOptional may be set empty.

Reproduced in: https: //juejin.im/post/5d01b809f265da1bc4144cdc

Guess you like

Origin blog.csdn.net/weixin_33858485/article/details/93169912