更改用于将短整型转换为布尔值的 EF6 源代码



如果布尔目标属性在 POCO 模型中用属性标记,那么修改映射代码以将值为零或非零的short转换为 false 或 true 的可行性是什么?

我的意思是,这应该是 EF 开源的优势之一,并且仅供内部使用。

任何关于我在代码中的位置的提示将不胜感激,但这个问题实际上更笼统,我想听听任何人对此的看法。

关于一般性评论。我不知道要更改 EF,但处理类似问题在 EF 中并不少见。EF 并非支持所有标准类型。

您可以在 POCO 类中有一个帮助程序字段。 因此,一个字段是实际的数据库字段,但在 POCO 之外没有使用。 帮助字段在流畅的 API 中不会被映射或忽略。 您可以通过助手访问数据库并执行任何所需的转换。 一个简单的例子。或者相反,如果我从前到前获得助手和数据库字段类型。

   [NotMapped]
    public virtual bool IsVisible { set; get; }  // Helper Field NOT on DB
    public int Test { get { return IsVisible ? 1 : 0; } //  on DB, but set and get via helper only.
                      set { IsVisible = (value != 0); }  }

编辑:Power Fluent API下面是一个代码片段,概述了如何以一致的方式为每个映射的 poco 运行代码。

 public class MyDbContext : DbContext 
// model building, set breakpoint so you know when this is triggered   
// it is important this ISNT called everytime, only on model cache.
// in my case that is app pool recycle.
protected override void OnModelCreating(DbModelBuilder modelBuilder)
    // use the CONFIG add feature to better organize and allow use of inheritance when mapping
    // I will use snippets and statics to keep it simple. 
        modelBuilder.Configurations.Add(XYZMap.Map()); //  POCO map 
        modelBuilder.Configurations.Add(ABCMAP.Map()); //  poco map
        modelBuilder.Configurations.Add(XXXMap.MAP()); //  poco map
    // etc for your POCO set
    // Note, no need to declare DBset<xyz> XYZs {get;set;} !!!!
     public static class XYZMap {
    public static BaseEntityIntConfiguration<PocoXYZ> Map() {
        //see return object !
        var entity = new BaseEntityLongConfiguration<PocoXYZ>();
         //entity.Property()...   // map away as usual POCO specifc
        ///entity.HasRequired()...// property and relationships as required
                                  // do nothing for default
        return entity;
       }
    }
}

 // all tables with int key use this base config. do it once never again
 public class BaseEntityIntConfiguration<T> : BaseEntityConfiguration<T> where T : BaseObjectInt {
    public BaseEntityIntConfiguration(DatabaseGeneratedOption DGO = DatabaseGeneratedOption.Identity) {
        // Primary Key
        this.HasKey(t => t.Id);
        // Properties
        //Id is an int allocated by DB
        this.Property(t => t.Id).HasDatabaseGeneratedOption(DGO); // default to db generated
        // optimistic lock is also added here, Specific to out poco design
        this.Property(t => t.RowVersion)
            .IsRequired()
            .IsFixedLength()
            .HasMaxLength(8)
            .IsRowVersion();
        // any other common mappings/ rules ?? 
    }
}
   public class BaseEntityConfiguration<T> : EntityTypeConfiguration<T> where T : BaseObject {
    public BaseEntityConfiguration() {
            this.ApplyAttributeRules();   // <<<<< Here is where I apply SYSTEM WIDE rules
    }
}

  public static void ApplyAttributeRules<T>(this EntityTypeConfiguration<T> entity) where T : BaseObject {
  // so this will be called for each mapped type
   foreach (var propertyInfo in typeof (T).GetProperties()) {
       // I use reflection to look for properties that meet certain criteria.
       // eg string.  I want as NVARCHAR 4000 not NVCAHR max so i can index it.
      if (propertyInfo.UnderLyingType().FullName == "System.String") {
                    SetStringLength(BosTypeTool.StringLengthIndexable, propertyInfo.Name, entity);
                    continue;
                }  
        SetStringLength(4000, propertyInfo.Name, entity); 
   }
 }
  private static void SetStringLength<TModelPoco>(int length, string propertyName,
        EntityTypeConfiguration<TModelPoco> entity) where TModelPoco : BaseObject {
        var propLambda = DynamicExpression.ParseLambda<TModelPoco, String>(propertyName);
        entity.Property(propLambda).HasMaxLength(length);
  // dynamic library from Microsoft.... http://msdn.microsoft.com/en-US/vstudio/bb894665.aspx
    }
 // get underlying type incase it is nullable
 public static Type UnderLyingType(this PropertyInfo propertyInfo) {
        return Nullable.GetUnderlyingType(propertyInfo.PropertyType) ??                  propertyInfo.PropertyType;
    }

最新更新