Built on the ASP. 2.0 WebAPI Background Framework (4)-EF core Codefirst database creation

Source: Internet
Author: User

Overview

In the article on the ASP. 2.0 WebAPI Background Framework (2)-EF core (MySQL) Codefirst database migration and Dependency injection, we show you how to quickly build a database in Codefirst quickly, in this chapter, we'll refine the creation of The validation and constraints that can be added in the library.

    • Microsoft Dad Official Document: Entity Framework Core

Database operations

(1) Database migration add-migration [any name, must be unique]

(2) Update database update-database

(3) Delete database migration remove-migration

Create a model,divided into data annotations andFluent API, the same effect, see personal habits two choose one

(1) primary key: By convention, the property name is Id or <type name>id will be configured as the key of the entity, or add [key] to specify

[Key]  Public string Get set; }

(2) Self-growth

[Databasegenerated (databasegeneratedoption.identity)]  Public Get set; }
Modelbuilder.entity<blog> (). Property (b=>b.inserted). Valuegeneratedonadd ();

(3) Required or optional properties

The following are allowed null: string, int, byte[], decimal? Wait

The following are not allowed null: int, decimal, bool , etc.

[Required]  Public string Get Set ; }
Modelbuilder.entity<blog> (). Property (b = B.url). IsRequired ();

(4) maximum length, only applicable to array data types, such as string and byte[]

[MaxLength (+)]  Public string Get set; }
modelbuilder.entity<Blog> (). Property (B=>b.url). Hasmaxlength (+);

(5) Custom data table name

[Table ("Dt_blog")]  Public class Blog {}
Modelbuilder.entity<blog> (). ToTable ("dt_blog");

(6) Custom column names

[Column ("Price")]  Public Get set; }

(7) Decimal Precision

[Column ("price", TypeName ="decimal (12,2)")]  Public decimal Get set; }
Modelbuilder.entity<person> (). Property (P=>p.price). Hascolumnname ("price"). Hascolumntype ("decimal (12,2)");;

(8) Batch modification accuracy, add the following code in onmodelcreating

foreach (varintypeof(decimaltypeof(decimal)        ))) {                "decimal (18,4)";        }    

(9) and the starter card

(MySQL)
[Concurrencycheck] Public Get set; }
(SQL Server) 1537269349  Public byte Get set; }
Modelbuilder.entity<person> (). Property (P=>p.rowversion). Isconcurrencytoken ();

(ten) hide attributes, which are undefined in your. NET entity class but are defined in the EF core model for that entity type

Modelbuilder.entity<blog> (). Property<datetime> ("lastupdated");

(11) The relationship, by convention, is created when the navigation property on the type is found. if the type it points to cannot be mapped to a scalar type by the current database provider, the property is treated as a navigation property.

For a longer length, please refer to the connection:https://docs.microsoft.com/zh-cn/ef/core/modeling/relationships

 Public classBlog { Public intBlogId {Get;Set; } Public stringURL {Get;Set; } PublicList<post> Posts {Get;Set; } } Public classPost { Public intPostID {Get;Set; } Public stringTitle {Get;Set; } Public stringContent {Get;Set; } Public intBlogId {Get;Set; } PublicBlog Blog {Get;Set; } } [ForeignKey ("Blogforeignkey")]  PublicBlog Blog {Get;Set; } [Inverseproperty ("Author")]  PublicList<post> authoredposts {Get;Set; } [Inverseproperty ("Contributor")]  PublicList<post> contributedtoposts {Get;Set; }
Modelbuilder.entity<post> (). HasOne (P=>p.blog). Withmany (b=>b.posts);

(12) Index

Modelbuilder.entity<blog> (). Hasindex (b = b.url). IsUnique (); Modelbuilder.entity<Person> (). Hasindex (p=>new{p.firstname,p.lastname});

(13) Exclude attributes

 Public classBlog { Public intBlogId {Get;Set; } Public stringURL {Get;Set; } PublicBlogmetadata Metadata {Get;Set; }} [Notmapped] Public classBlogmetadata { PublicDateTime Loadedfromdatabase {Get;Set; }}
Modelbuilder.ignore<blogmetadata> ();

(14) Type of exclusion

 Public class Blog {publicintgetset;}  Public string Get Set ; } [Notmapped]  Public Get Set ; }}
Modelbuilder.entity<blog> (). Ignore (b = b.loadedfromdatabase);

Based on the ASP. 2.0 WebAPI Background Framework build (4)-EF core Codefirst database creation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.