NopCommerce用.net core重写ef
? 最近看了NopCommerce源碼,用core學習著寫了一個項目,修改的地方記錄下。項目地址
? NopCommerce框架出來好久了。18年的第一季度 懶加載出來后也會全部移動到.net core。那么就更好玩了。
?
? 項目內容
? ? ? 當然NopCommerce還包含很多特技:Plugin,Seo,訂閱發布,theme切換等等。這些后期再維護進去。
? ?項目介紹
Nop.Core:【核心層】基礎設施,例:領域對象,倉庫接口,引擎接口,DI管理接口,反射,公共方法。
Nop.Data:【數據層】EF相關,dbcontext,倉儲實現,mapping
Nop.Services:【服務層】數據邏輯處理由這層提供。
Nop.Web:【頁面層】展示界面。
Nop.Web.Framework:【頁面基礎層】web層的上層封裝。例如啟動項的實現,DI實現。
? ? ? ? ? ? ? ? ?詳細的分層思想和細節這里不再復述。
?
2.項目修改點
? ? ? ? ? ? EF6轉換到EFCore,數據庫選擇mysql,動態加載dbset 看了相關代碼,在進行解釋
1 public class DataBaseStartup: ISelfStartup 2 { 3 4 public void ConfigureServices(IServiceCollection services, IConfigurationRoot configuration) 5 { 6 services.AddDbContext<SelfDbContext>(x => x.UseMySql(configuration.GetConnectionString("MySql"))); 7 } 8 9 public void Configure(IApplicationBuilder application) 10 { 11 var dbContext=EngineContext.Current.ServiceProvider.GetService<SelfDbContext>(); 12 dbContext.Database.EnsureCreated(); 13 } 14 15 public int Order { get; } = 1; 16 } View Code public SelfDbContext(DbContextOptions options) : base(options){}protected override void OnModelCreating(ModelBuilder modelBuilder){modelBuilder.AddEntityConfigurationsFromAssembly(GetType().Assembly);base.OnModelCreating(modelBuilder);} View Code 1 public static class ModelBuilderExtenions 2 { 3 private static IEnumerable<Type> GetMappingTypes(this Assembly assembly, Type mappingInterface) 4 { 5 return assembly.GetTypes().Where(x => !x.IsAbstract && !x.IsGenericType && !x.IsInterface && x.GetInterfaces().Any(y => y == mappingInterface)); 6 } 7 8 public static void AddEntityConfigurationsFromAssembly(this ModelBuilder modelBuilder, Assembly assembly) 9 { 10 var mappingTypes = assembly.GetMappingTypes(typeof(ISelfEntityMappingConfiguration)); 11 foreach (var config in mappingTypes.Select(Activator.CreateInstance).Cast<ISelfEntityMappingConfiguration>()) 12 { 13 config.Map(modelBuilder); 14 } 15 } 16 } View Code 1 public class StudentMapping : ISelfEntityMappingConfiguration 2 { 3 public void Map(ModelBuilder b) 4 { 5 b.Entity<Student>().ToTable("Student") 6 .HasKey(p => p.Id); 7 8 b.Entity<Student>().Property(p => p.Class).HasMaxLength(50).IsRequired(); 9 b.Entity<Student>().Property(p => p.Name).HasMaxLength(50); 10 } 11 } View Code 1 public interface ISelfEntityMappingConfiguration 2 { 3 void Map(ModelBuilder b); 4 } View Code 1 public class Student:BaseEntity 2 { 3 public string Name { get; set; } 4 5 public string Class { get; set; } 6 } View Code解釋:
3.項目繼承nopcommerce的其他地方
??
轉載于:https://www.cnblogs.com/TeemoHQ/p/8343731.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的NopCommerce用.net core重写ef的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C语言有限域的构造,有限域(3)——多项
- 下一篇: get_metrology_object