首页 / C#开发 / 正文

[C#/.NET]Entity Framework(EF) Code First 使用反射加载程序集中的Fluent API配置文件

6130 发布于: 2015-11-24 读完约需8分钟
csharp 在C#/.NET的Entity Framework Code First开发中,我们会涉及到关于数据库连接的配置类,该类会继承至DbContext类,其主要目的是配置数据库的连接字符串或者名字以及相关的其他参数,如果我们需要重写DbContext的OnModelCreating()的方法并加载实体的Fluent API配置文件的话,一般可以使用如下的方法:
modelBuilder.Configurations.Add(new UserConfiguration());
其中UserConfiguration即为User类对应的Fluent API配置文件类,该类继承至:EntityTypeConfiguration类,为了方面大家阅读起来更容易,我把UserConfiguration的配置代码贴出来,如下:
public class UserConfiguration : EntityTypeConfiguration<User>
    {
        public UserConfiguration()
        {
            Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            Property(x => x.Guid).HasMaxLength(50).IsRequired();
            Property(x => x.LoginName).HasMaxLength(50).IsRequired().HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("IX_LoginName", 1) { IsUnique = true }));
            Property(x => x.Password).HasMaxLength(80).IsRequired();
            Property(x => x.DisplayName).HasMaxLength(80).IsRequired();
            Property(x => x.EmailAddress).HasMaxLength(120).IsRequired();
            Property(x => x.FirstName).HasMaxLength(80);
            Property(x => x.LastName).HasMaxLength(80);
            Property(x => x.CreatedBy).HasMaxLength(80);
            Property(x => x.ModifiedBy).HasMaxLength(80);
            Property(x => x.LatestIp).HasMaxLength(36);
        }
    }
其中EntityTypeConfiguration<User>中的User为实体类,具体的配置选项可根据自己的需求来定制。 写到这里,我们以上表现的内容还没有和本文的主题产生联系。那么,我们现在来看一个问题,即:在项目中每增加一个实体类对应的EntityTypeConfiguration<T>的配置文件,我们就需要在OnModelCreating的重写方法中新添加一行对应的配置行:
modelBuilder.Configurations.Add(new Configuartion());
这样做肯定是没有问题的,但这样的做法效率太低,而且不好维护。那么,下面我们就利用反射来动态加载Fluent API文件到OnModelCreating的重写方法中去,具体的实现代码如下:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

            //modelBuilder.Configurations.Add(new UserConfiguration());
            var typesToRegister = Assembly.GetExecutingAssembly().GetTypes()
              .Where(x => !string.IsNullOrEmpty(x.Namespace))
              .Where(x => x.BaseType != null && x.BaseType.IsGenericType && x.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>));
            foreach (var type in typesToRegister)
            {
                dynamic configurationInstance = Activator.CreateInstance(type);
                modelBuilder.Configurations.Add(configurationInstance
                  );
            }
        }
这样,我们不管以后写多少个Fluent API的配置文件,程序都会自动为我们加载,是不是很方便的一个小技巧呢?

版权声明:本作品系原创,版权归码友网所有,如未经许可,禁止任何形式转载,违者必究。

上一篇: C#/.NET Entity Framework(EF) Code First Repository泛型类中只更新实体的指定字段(属性)

下一篇: 分享一个Javascript(js)生成不重复随机数组的函数

本文永久链接码友网 » [C#/.NET]Entity Framework(EF) Code First 使用反射加载程序集中的Fluent API配置文件

分享扩散:

发表评论

登录用户才能发表评论, 请 登 录 或者 注册