实体框架映射问题

时间:2015-11-11 20:32:57

标签: entity-framework-6

在我的EF代码中,我使用基于EntityTypeConfiguration<T>类的类来执行配置。

在这些类中,我有辅助方法来帮助配置。例如,我有以下代码:

protected void MapGuidColumn(Expression<Func<T, Guid>> selector, string columnName, bool isRequired, bool isIdentity)
{
  PrimitivePropertyConfiguration configuration = null;

  configuration = this.Property(selector);
  configuration.HasColumnName(columnName);

  if (isRequired == true)
  {
    configuration.IsRequired();
  }
  else
  {
    configuration.IsOptional();
  }

  if (isIdentity == true)
  {
    configuration.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
  }

  return;
}

我使用如下语句调用此方法:

MapGuidColumn(e => e.ID, "ID", true, true);

MapGuidColumn方法中,实际收到的表达式为e => Convert(e).ID而非e => e.ID。 EF似乎在语句configuration = this.Property(selector);上有这个表达式的问题。它失败并显示以下异常消息:

  

System.InvalidOperationException:表达式'e =&gt;转换(e).ID'不是有效的属性表达式。表达式应代表一个属性:C#:'t =&gt; t.MyProperty'VB.Net:'Punction(t)t.MyProperty'。对嵌套属性使用虚线路径:C#:'t =&gt; t.MyProperty.MyProperty'VB.Net:'Punction(t)t.MyProperty.MyProperty'。

我过去曾使用过这种方法,但从未真正看过辅助方法接收到的表达式。但是,这是我第一次遇到这个问题。

有人能指出我可能会发生什么的方向吗?如有必要,我可以提供更多代码(我只是不确定此时提供什么)。

0 个答案:

没有答案