Creating classes in Efcore implements the partner class implementation of the entity class by implementing the Ientitytypeconfiguration<t> interface Fluentapi

Source: Internet
Author: User

1 Creating an Entity class:

  Public Partial classNewscategory:iaggregationroot { Publicnewscategory () {} PublicGuid Id {Get;Set; }  Public stringCategoryName {Get;Set; }  Public BOOLIsdel {Get;Set; }  Public stringCode {Get;Set; } }

2. Create a mapping partner class for an entity class

 Public class Newscategorymap:ientitytypeconfiguration<newscategory>    {        publicvoid Configure (entitytypebuilder<newscategory> builder)        {            Builder. ToTable ("newscategory");        }    }

3. Override the Onmodelcreating method in context to apply the partner class

  Public classNewsefcorecontext:dbcontext,inewscontext { PublicDbset<newscategory> Categories {Get;Set; } protected Override voidonconfiguring (Dbcontextoptionsbuilder optionsbuilder) {//write the connection string directly and build the database. Later configured in the configuration file and used hereOptionsbuilder.usesqlserver ("Server=localhost;database=wehope; User Id=sa; Password=0"); //Optionsbuilder.usesqlserver (appsetting.getappsetting ("Dealercontext"));        }        protected Override voidonmodelcreating (ModelBuilder ModelBuilder) {modelbuilder.applyconfiguration<NewsCategory> (NewNewscategorymap ()); Base.         Onmodelcreating (ModelBuilder); }    }

Complete.

Creating classes in Efcore implements the partner class implementation of the entity class by implementing the Ientitytypeconfiguration<t> interface Fluentapi

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.