2017-03-29 62 views
1

我正在使用實體框架來映射我們公司的軟件',我想創建一種基於我創建的字段屬性的條件字段映射。試圖更好地解釋: 我已經創建的屬性ModelPersistentAttribute我以下列方式使用:實體框架6 - 字段條件映射

[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; } 
    } 

然後創建一個具有自動映射方法,該方法如下

/// <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 =>值(DTO.Service.EFCore.EntityTypeVersionMap` 1+ <> c__DisplayClass4_0 [TestDTO.Shared.WorkOrderDTO])。propertyInfo'不是有效的屬性表達式。表達式應該表示一個屬性:C#:'t => t.MyProperty'VB.Net:'Function(t)t.MyProperty'。'

對此的任何幫助將非常感謝! 在此先感謝 路易吉

回答

1

可以使用非通用TypesWhere,這Configure - >Ignore方法對string propertyNamePropertyInfo propertyInfo(你所需要的一個)過載:

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