Entity Framework Mapping Issue

好久不见. 提交于 2019-12-12 05:27:56

问题


In my EF code, I am using classes based on the EntityTypeConfiguration<T> class to perform configuration.

In these classes, I have helper methods to aid in the configuration. For example, I have the following code:

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

I call this method using a statement like the following:

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

In the MapGuidColumn method, the expression that is actually received is e => Convert(e).ID and not e => e.ID. EF seems to have issues with this expression on the statement configuration = this.Property(selector);. It fails with the following exception message:

System.InvalidOperationException: The expression 'e => Convert(e).ID' is not a valid property expression. The expression should represent a property: C#: 't => t.MyProperty' VB.Net: 'Function(t) t.MyProperty'. Use dotted paths for nested properties: C#: 't => t.MyProperty.MyProperty' VB.Net: 'Function(t) t.MyProperty.MyProperty'.

I have used this method in the past but never really looked the expression that is received by the helper methods. However, this is the first time I have run into this issue.

Can someone point me in the direction as to what might be happening here? I can provide more code if necessary (I am just not sure what to provide at this point).

来源:https://stackoverflow.com/questions/33659311/entity-framework-mapping-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!