首页 > 数据库 >EF Core自动将实体映射到数据库

EF Core自动将实体映射到数据库

时间:2023-02-05 00:23:38浏览次数:43  
标签:Core 映射 EF modelBuilder && var new Type

 protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            try
            {
                var compilationLibrary = DependencyContext.Default.CompileLibraries.Where(x => !x.Serviceable && x.Type == "project" && x.Type != "package");
                foreach (var item in compilationLibrary)
                {
                    AssemblyLoadContext.Default.LoadFromAssemblyName(new System.Reflection.AssemblyName(item.Name))
                        .GetTypes().Where(x => !x.IsAbstract && x.BaseType != null && x.IsAssignableTo(typeof(BaseEntity)))
                        .ToList()
                        .ForEach(x =>
                        {
                            modelBuilder.Entity(x);
                        });
                }
            }
            catch (Exception e)
            {

                throw new InvalidOperationException(e.Message);
            }
            base.OnModelCreating(modelBuilder);
        }

 

标签:Core,映射,EF,modelBuilder,&&,var,new,Type
From: https://www.cnblogs.com/hljjway/p/17092696.html

相关文章