Repositories. EntityFramework implementation method, entityframework
Record your EntityFramework data access layer today. Data access is implemented through the generic Repository method. First, create a structure chart.
The classes in the Configuration folder are all object ing classes. All these classes are inherited to the EntityConfigurationBase class.
EntityConfigurationBase inherits to the EntityTypeConfiguration class, which is the entity ing base class of EntityFramework.
1 using System. data. entity. modelConfiguration; 2 using System. data. entity. modelConfiguration. configuration; 3 4 using ZY. core. entities; 5 6 namespace ZY. repositories. entityFramework 7 {8 /// <summary> 9 // data entity ing configuration base class 10 /// </summary> 11 /// <typeparam name = "TEntity"> </typeparam> 12 // <typeparam name = "TKey"> </typeparam> 13 public abstract class EntityConfigurationBase <TEntity, TKey>: EntityTypeConfiguration <TEntity>, IEntityMapper14 where TEntity: class15 {16 // ing entity added to data context 17 public void RegistorTo (ConfigurationRegistrar configurations) 18 {19 configurations. add (this); 20} 21} 22}View Code
An important method here is RegistorTo (ConfigurationRegistrar configurations). This method adds the current object to the data context. In this way, you do not need to write ing between each object in the data context to decouple the object from the context. Reflection is used in the OnModelCreating (DbModelBuilder modelBuilder) method, and all object ing relationships are added to the data context through reflection. The optimization here is that cache can be used for reflection.
1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. threading. tasks; 6 using System. data. entity; 7 using System. data. entity. modelConfiguration. conventions; 8 using System. reflection; 9 10 namespace ZY. repositories. entityFramework11 {12 public class BaseDbContext: DbContext13 {14 public BaseDbContext () 15: base ("Default") 16 {} 17 18 public Ba SeDbContext (string connectionString) 19: base (connectionString) 20 {} 21 22 protected override void OnModelCreating (DbModelBuilder modelBuilder) 23 {24 // disable cascading deletion 25 modelBuilder. conventions. remove <OneToManyCascadeDeleteConvention> (); 26 // obtain all ing object classes 27 IEnumerable <IEntityMapper> entityMappers = GetEntityMappers (). select (type => Activator. createInstance (type) as IEntityMapper ). toList (); 28 29 foreach (IEnt ItyMapper mapper in entityMappers) 30 {31 mapper. registorTo (modelBuilder. ations ); 32} 33} 34 35 // <summary> 36 // the best way to obtain all object ing objects through reflection is to save it in the cache. 37 // </summary> 38 /// <returns> </returns> 39 private Type [] GetEntityMappers () 40 {41 Type [] mapperTypes = Assembly. getExecutingAssembly (). getTypes () 42. where (type =>! String. IsNullOrEmpty (type. Namespace) 43. Where (type => type. BaseType! = Null & type. baseType. isGenericType & 44 type. baseType. getInterface (typeof (IEntityMapper ). name) = typeof (IEntityMapper )). toArray (); 45 return mapperTypes; 46} 47} 48}View Code
Automatic migration is used for data migration. In the past, automatic migration was not used when EF was used. many pitfalls have been encountered. Since automatic migration was used, migration has never been taken care.
1 using System. data. entity. migrations; 2 3 4 namespace ZY. repositories. entityFramework. migrations 5 {6 /// <summary> 7 // automatic migration settings 8 /// </summary> 9 public class AutoMigrationsConfiguration: dbMigrationsConfiguration <BaseDbContext> 10 {11 public AutoMigrationsConfiguration () 12 {13 AutomaticMigrationsEnabled = true; // automatically migrates 14 AutomaticMigrationDataLossAllowed = true; // data loss is allowed. 15} 16} 17}View Code
The Repository code has been posted in the previous article. Asynchronous and synchronous methods are implemented.
The entire code will be put on github in the future