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