Simple use of the Entity framework

Source: Internet
Author: User

The company's project in the EF, the recent time to study the next, collated a more common EF framework for everyone to share.

EF This east, use good words, really convenient development, use bad words, out of a problem half a day can not find out what is the reason.

Let's introduce the simple use of EF. Mainly divided into the following 5 items

Ef. Core: Data Entity

Ef. data:c# entities directly mapped to data tables

Ef. Service: Data Services

Efframework: public class Library

Efsolution: Test Project

DbContext is an important class for EF, and our database access objects must inherit that class. Here's the data context we're going to use:

 Public classEfobjectcontext:dbcontext, Idbcontext { PublicEfobjectcontext (stringnameorconnectionstring):Base(nameorconnectionstring) {//((Iobjectcontextadapter) this). ObjectContext.ContextOptions.LazyLoadingEnabled = true;        }        protected Override voidonmodelcreating (Dbmodelbuilder modelBuilder) {//dynamically load all configuration//System.Type configtype = typeof (Languagemap); //Any of your configuration classes here//var typestoregister = assembly.getassembly (Configtype). GetTypes ()            string[] MapPath = {"EF. Data.dll" }; foreach(varPathinchMapPath) {                varASM =Assembly.LoadFrom (path); varTypestoregister =ASM. GetTypes (). Where (Type= =!string.isnullorempty (type. Namespace)). Where (Type= = Type. BaseType! =NULL&& type. Basetype.isgenerictype && type. Basetype.getgenerictypedefinition () = =typeof(entitytypeconfiguration<>)); foreach(varTypeinchtypestoregister) {Dynamic Configurationinstance=activator.createinstance (type);                MODELBUILDER.CONFIGURATIONS.ADD (configurationinstance); }            }            //... or do it manually below. For example,//ModelBuilder.Configurations.Add (New Languagemap ());            Base.        Onmodelcreating (ModelBuilder); }}
View Code

The Onmodelcreating method is when an application loads a map of an entity and a data table into the data context at initialization time.

1. Defining entities

     Public class bloguser:baseentity    {        publicstringgetset;}          Public int Get Set ; }          Public string Get Set ; }    }
View Code

It's easier here, no more nonsense.

2. Define Mappings

 Public Partial class Blogusermap:entitytypeconfiguration<bloguser>    {        public  blogusermap ()        {             this. ToTable ("bloguser");             this. Haskey (c = c.id);        }    }
View Code

Define the mapping of entities to data tables, and a pair of one or one-to-many, many-to-many relationships between entities is also defined here.

3. Define Data Service

 Public Interface Ibloguserservice    {        IList<BlogUser> getallbloguser ();    }
View Code
 public  class   Bloguserservice:ibloguserservice { private  readonly  irepository<bloguser> _        Bloguserrepository;  public  bloguserservice (irepository<bloguser> Bloguserrepository) { this . _bloguserrepository = Bloguserrepository;  public  ilist<bloguser> Getallbloguser () { return   _blo        GUserRepository.Table.ToList (); }    }
View Code

The test procedure is as follows:

Static void Main (string[] args)        {            enginecontext.initialize (false);            Console.WriteLine (" initialization complete ");             = Enginecontext.current.resolve<ibloguserservice>();            IList<BlogUser> users = service. Getallbloguser ();            Console.readkey ();        }
View Code

This is the simple use of EF.

Simple use of the Entity framework

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.