实体框架6 - 字段条件映射

时间:2017-03-29 07:16:15

标签: entity-framework

我正在使用Entity Framework来映射我们公司的软件'以及我想根据我创建的一些字段属性创建一种条件字段映射。试图更好地解释: 我创建了以下列方式使用的ModelPersistentAttribute属性:

    [ModelPersistent("WORKORDER", persistentSchema: "mySchema", fromVersion:       "0.0", toVersion: "2.0")]
    public class WorkOrderDTO : AMOSEntityDTO
    {
     public WorkOrderDTO()
     { }

     public WorkOrderDTO(decimal WORKORDERID, string WONO, DateTime    LASTUPDATED)
     {
       this.WORKORDERID = WORKORDERID;
       this.WONO = WONO;
       this.LASTUPDATED = LASTUPDATED;
     }

     [Key]
     public decimal WORKORDERID { get; set; }

     public string WONO { get; set; }

     [ModelPersistentAttribute(persistentName: "TITLE", fromVersion:"0.0")]
     public string myTITLE { get; set; }
   }

然后我创建了一个具有automap方法的映射类,该方法如下

    /// <summary>
    /// Automapping
    /// </summary>
    /// <param name="configurationOptions"></param>
    protected void AutoMap(EntityTypeMapCondigurationOptionsEFNet<TEntity> entityConfigurationOptions)
    {
        IEnumerable<ModelPersistentAttribute> persistentAttributes = typeof(TEntity).GetCustomAttributes<ModelPersistentAttribute>();
        if (persistentAttributes.Count() == 0 || persistentAttributes.Any(mpa => mpa.IsInRange(entityConfigurationOptions.Version, entityConfigurationOptions.DBMSType)))
        {
            if (persistentAttributes.Count() != 0) MapEntity(entityConfigurationOptions, persistentAttributes);
            foreach (var prop in typeof(TEntity).GetProperties())
            {
                NotMappedAttribute notMapped = prop.GetCustomAttribute<NotMappedAttribute>();
                if (notMapped == null)
                {
                    IEnumerable<ModelPersistentAttribute> modelAttributes = prop.GetCustomAttributes<ModelPersistentAttribute>();
                    if (modelAttributes.Count() == 0 || (modelAttributes.Any(ma => ma.IsInRange(entityConfigurationOptions.Version))))
                        MapProperty(prop, entityConfigurationOptions, modelAttributes);
                    else
                        IgnoreProperty(prop, entityConfigurationOptions);
                }
            }
        }
        else
            IgnoreEntity(entityConfigurationOptions);
    }

一切似乎都适用于映射

    protected virtual void MapProperty(PropertyInfo propertyInfo, EntityTypeMapCondigurationOptionsEFNet<TEntity> entityConfigurationOptions, IEnumerable<ModelPersistentAttribute> modelAttributes)
    {
        ModelPersistentAttribute modelVersionAttribute = modelAttributes.FirstOrDefault<ModelPersistentAttribute>(mpa => mpa.IsInRange(entityConfigurationOptions.Version, entityConfigurationOptions.DBMSType));
        string persistentName = string.Empty;
        if (modelVersionAttribute != null)
            persistentName = modelVersionAttribute.PersistentName;
        else
            persistentName = propertyInfo.Name;
        entityConfigurationOptions.ModelBuilder.Properties().Where(p => p.Equals(propertyInfo)).Configure(c=> c.HasColumnName(persistentName));
    }

但是当我想忽略一个属性时,我无法运行它:

    /// <summary>
    /// Ignore property
    /// </summary>
    /// <param name="propertyInfo"></param>
    protected virtual void IgnoreProperty(PropertyInfo propertyInfo, EntityTypeMapCondigurationOptionsEFNet<TEntity> entityConfigurationOptions)
    {
        entityConfigurationOptions.ModelBuilder.Types<TEntity>().Configure(ctc => ctc.Ignore(p => propertyInfo));

 // The following as well doesn't work
 //entityConfigurationOptions.ModelBuilder.Types<TEntity>//().Configure(ctc =>    ctc.Ignore(p => propertyInfo.Name));

    }

我得到的错误的含义是我理解的,但我不知道如何解决:

表达式'p =&gt; value(DTO.Service.EFCore.EntityTypeVersionMap`1 +&lt;&gt; c__DisplayClass4_0 [TestDTO.Shared.WorkOrderDTO])。propertyInfo'不是有效的属性表达式。表达式应代表一个属性:C#:'t =&gt; t.MyProperty'VB.Net:'Punction(t)t.MyProperty'。'

对此的任何帮助都将非常感谢! 提前致谢 路易

1 个答案:

答案 0 :(得分:1)

您可以将Types方法与Where一起使用Configure - &gt; Ignore方法有string propertyNamePropertyInfo propertyInfo(您需要的那个)的重载:

entityConfigurationOptions.ModelBuilder
    .Types().Where(type => type == typeof(TEntity))
    .Configure(ctc => ctc.Ignore(propertyInfo));