zoukankan      html  css  js  c++  java
  • .net Core EF统一配置实体类型

    一般情况需要对某个实体进行一些配置时代码如下:

    protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                base.OnModelCreating(modelBuilder);
                modelBuilder.Entity<Product>().Property(p => p.Name).HasMaxLength(50).IsRequired();
            }

    如果按照上面代码对实体类型进行配置,当实体Entity特别多时难免不便于维护,如果能自动加载执行实体配置的相关类再好不过了

    以下代码即可实现统一加载实体配置类并执行

    using Microsoft.EntityFrameworkCore;
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.EntityFrameworkCore.Metadata.Builders;
    using System.Reflection;
    using System.Linq;
    
    namespace DAL.Mapping
    {
        public interface IEntityMappingConfiguration
        {
            void Map(ModelBuilder b);
        }
    
        public interface IEntityMappingConfiguration<T> : IEntityMappingConfiguration where T : class
        {
            void Map(EntityTypeBuilder<T> builder);
        }
    
        public abstract class EntityMappingConfiguration<T> : IEntityMappingConfiguration<T> where T : class
        {
            public abstract void Map(EntityTypeBuilder<T> b);
    
            public void Map(ModelBuilder b)
            {
                Map(b.Entity<T>());
            }
        }
    
        public static class ModelBuilderExtenions
        {
            private static IEnumerable<Type> GetMappingTypes(this Assembly assembly, Type mappingInterface)
            {
                return assembly.GetTypes().Where(x => !x.IsAbstract && x.GetInterfaces().Any(y => y.GetTypeInfo().IsGenericType && y.GetGenericTypeDefinition() == mappingInterface));
            }
    
            public static void AddEntityConfigurationsFromAssembly(this ModelBuilder modelBuilder, Assembly assembly)
            {
                var mappingTypes = assembly.GetMappingTypes(typeof(IEntityMappingConfiguration<>));
                foreach (var config in mappingTypes.Select(Activator.CreateInstance).Cast<IEntityMappingConfiguration>())
                {
                    config.Map(modelBuilder);
                }
            }
        }
    }

    使用:

    using Entity.Table;
    using Microsoft.EntityFrameworkCore;
    using Microsoft.EntityFrameworkCore.Metadata.Builders;
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace DAL.Mapping
    {
        public class ProductConfiguration : EntityMappingConfiguration<Product>
        {
            public override void Map(EntityTypeBuilder<Product> b)
            {
                b.ToTable("Product", "HumanResources")
                    .HasKey(p => p.Id);
    
                b.Property(p => p.Name).HasMaxLength(50).IsRequired();
            }
        }
    }

    自动加载加载并执行继承了EntityMappingConfiguration的类达到统一对实体类型进行配置的目的

    protected override void OnModelCreating(ModelBuilder modelBuilder)
            {            
                base.OnModelCreating(modelBuilder);
                modelBuilder.AddEntityConfigurationsFromAssembly(GetType().Assembly);
            }

     参考:

    https://stackoverflow.com/questions/26957519/ef-core-mapping-entitytypeconfiguration#comment46244976_26957519
  • 相关阅读:
    Python all() 函数
    支付流程及安全测试(转载)
    qtp 自动化测试--点滴 菜单没有了,有些控件运行时找不到
    qtp 自动化测试---点滴 获取属性性/修改窗体标题
    点滴记录1-学习-问题-原因-解决方法
    测试职业规划 (转载)
    sql注入测试用例
    转载--测试员要想实现自己的职业发展目标,离不开这5点
    qtp 自动化测试--点滴 自定义显示工具菜单 trzedit
    QTP 自动化测试--定义变量
  • 原文地址:https://www.cnblogs.com/xiaoliangge/p/7657925.html
Copyright © 2011-2022 走看看