Entity Framework - changing navigation property name generates schema validation error (edmx)

Asked

Viewed 27 times

0

We have a . net system that uses Entity Framework 6 (database first). Some tables have more than one fk for another table. Entity, when generating navigation properties for these relationships, names them with an incremental numeral suffix. Ex (Status and Table tables):

Status
cd_status
description

Tabub
id
name
cd_status_initial (FK -> Status.cd_status)
cd_status_final (FK -> Status.cd_status)

EF6 generates the entitade referring to Tabelab, as follows:

public partial class TabelaB
{
  public int32 id { get; set; }
  public string nome { get; set; }
  public int32 cd_status_inicial {get; set; }
  public int32 cd_status_final {get; set; }

  public Status Status { get; set; }
  public Status Status1 { get; set; }
}

The names of the navigation properties do not have the nomenclature that facilitates the understanding of the code. By changing the T4 template (.tt), I was able to make properties with more appropriate names:

public partial class TabelaB
{
  public int32 id { get; set; }
  public string nome { get; set; }
  public int32 cd_status_inicial {get; set; }
  public int32 cd_status_final {get; set; }

  public Status Status_inicial { get; set; }
  public Status Status_final { get; set; }
}

It happens that this change generates validation error in the edmx schema, when it is going to execute some query through Entity, because in edmx there are the names "Status" and "Status1", which no longer exists.

Is there any way to customize the creation of the edmx file to generate the names of the navigation properties in the same way as the entity classes?

  • Instead of renaming only in this class you need to update all references, try renaming by selecting the property with two clicks and then hold Ctrl and press R twice. If this does not resolve, you need to review your mappers

  • Leandro, the change of the name of the properties I made through the T4 template (*.tt) that Entity generates. I made the change in the template so that in the next database updates, when I update edmx ("Update Model from Database), the classes are already generated with new default name. And this worked. It happens that when Entity will execute some query it does a validation of the edmx fields relative to the classes and as the names are different it returns the error. Changing the field names, in hand, in edmx the application works. I’m looking for a way for it to be automatic as well.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.