Entity Framework Mapping Issue

2019-09-04 05:55发布

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).

0条回答
登录 后发表回答