EF Code First 使用反射加载程序集中的Fluent API配置文件

在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为实体类,具体的配置选项可根据自己的需求来定制。 写到这里,我们以上表现的内容还没有和本文的主题产生联系。那么,我们现在来看一个问题,即:在项目中每增加一个实体类对应的EntityTypeConfiguration的配置文件,我们就需要在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的配置文件,程序都会自动为我们加载,是不是很方便的一个小技巧呢?


EF Code First Repository泛型类中只更新实体的指定字段(属性)

在C#的Entity Framework ORM中,如果使用context.Entry(T).State=EntityState.Modified;来更新实体的话,整个实体的字段都将被更新。那么,如果我们在开发中只需要更新部分指定的字段应该如何操作呢?一种简单的实现方式就是使用表达式树作为更新方法的参数,然后使用Property(T).IsModified=true来对指定字段进行更新,具体的代码如下:

public virtual int Update(T entity, params Expression<Func<T, object>>[] updatedProperties)
    {
      var dbEntityEntry = dataContext.Entry(entity);
      if (updatedProperties.Any())
      {
        foreach (var property in updatedProperties)
        {
          dbEntityEntry.Property(property).IsModified = true;
        }
      }
      else
      {
        foreach (var property in dbEntityEntry.OriginalValues.PropertyNames)
        {
          var original = dbEntityEntry.OriginalValues.GetValue<object>(property);
          var current = dbEntityEntry.CurrentValues.GetValue<object>(property);
          if (original != null && !original.Equals(current))
          {
            dbEntityEntry.Property(property).IsModified = true;
          }
        }
      }
      return dataContext.SaveChanges();
    }

调用就更简单了,如:

_userRepository.Update(user,x=>x.Enabled);

其中的Enabled即为user对应实体类的一个属性。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值