Entity Framework is not interpreting my settings

Asked

Viewed 318 times

4

I have a serious problem with Entity Framework.

I have two tables: Módulos and Viaturas, both with primary key name ID (Database First).

A car can have a Module, but it can also be null, the association is not mandatory, however I have a FK in the Table Viaturas for the Table Módulos, summarizing the tables were created like this:

CREATE TABLE MODULOS
(
    ID NUMBER NOT NULL,

    CONSTRAINT PK_MODULOS PRIMARY KEY (ID)
);

CREATE TABLE VIATURAS
(
    ID NUMBER NOT NULL,

    PLACA VARCHAR2(10) NOT NULL,

    ID_MODULO NUMBER,

    CONSTRAINT PK_VIATURAS PRIMARY KEY (ID),

    CONSTRAINT FK_VIATURAS1 FOREIGN KEY (ID_MODULO) REFERENCES MODULOS (ID)
);

I tried to make the relationship in several ways, always unsuccessful in the Entity.

Using only Data Annotations...

Modulo class:

[Table("MODULO")]
public partial class Modulo
{
    [Key]
    [Column("ID", TypeName = "NUMBER")]
    public long ModuloID { get; set; }

    public virtual Viatura Viatura { get; set; }
}

Class Car:

[Table("VIATURA")]
public partial class Viatura
{
    [Key, ForeignKey("Modulo")]
    [Column("ID", TypeName = "NUMBER")]
    public long ViaturaID { get; set; }

    [Column("PLACA", TypeName = "VARCHAR2")]
    public string Placa { get; set; }

    [Column("ID_MODULO", TypeName = "NUMBER")]
    public long? ModuloID { get; set; }

    public virtual Modulo Modulo { get; set; }
}

When I run the query, Entity does not perform the comparison

VIATURA.ID_MODULO = MODULO.ID

Instead, it performs:

VIATURA.ID = MODULO.ID

FROM "VIATURA" "Extent1"
INNER JOIN "MODULO" "Extent2" ON "Extent1"."ID" = "Extent2"."ID"

Using only Fluent API:

Config Modulo:

HasKey(t => t.ModuloID);

Property(b => b.ModuloID).HasColumnName("ID");

Config Carro:

HasKey(c => c.ViaturaID);

Property(p => p.ViaturaID).HasColumnName("ID");

HasOptional(t => t.Modulo);

When I run the query, Entity also performs the comparison:

VIATURA.ID = MODULO.ID

Forcing ID_MODULO column for relationship in Fluent API:

Config Carro:

HasKey(c => c.ViaturaID);

Property(p => p.ViaturaID).HasColumnName("ID");

HasRequired(h => h.Modulo).WithOptional().Map(m => m.MapKey("ID_MODULO"));

Config Modulo:

HasKey(t => t.ModuloID);

Property(b => b.ModuloID).HasColumnName("ID");

In this scenario, the relationship worked, but then the Entity freaked out, he now thinks that the column ID of the CAR Table is called Viaturaid, and I said that the column name is ID.

{"ORA-00904: "Extent2 ". " Viatura_viaturaid ": identifier invalid"}

I’m using the latest version of Entity (6.2.0) in Dotnet Framework 4.5.

Thanks in advance.

  • 1

    It is not the solution but it can help to improve the understanding of your code, try to map the tables with Entitytypeconfiguration, you are doing a fluency mixApi with Datanotations ta kind of complex that ai.

  • Foreignkey("Module")] is a foreign Moduloid key and not a Car ID.

  • No Mark, I did it separately, first I did it only with Data Annotation and then only with Fluent API.

  • I figured out the problem, but I didn’t get it. Of the Modulo class, I removed the statement: public virtual Vehicle Vehicle { get; set; } E worked. But I understand that the statement can be in both ways (classes), because I may want to select a Module and also return the associated Vehicle.

  • @Albertoluizschaub the relationship of the Entity is always done in one of the two entities. That is a rule. However, this does not prevent you from navigating differently. That is: You make the relationship once and use the way you want.

1 answer

0

[Foreignkey("Module")] is a foreign Moduloid key and not a Car ID.

[Table("VIATURA")]
public partial class Viatura
{
    [Key]
    [Column("ID", TypeName = "NUMBER")]
    public long ViaturaID { get; set; }

    [Column("PLACA", TypeName = "VARCHAR2")]
    public string Placa { get; set; }

    [Column("ID_MODULO", TypeName = "NUMBER")]
    [ForeignKey("Modulo")]
    public long? ModuloID { get; set; }

    public virtual Modulo Modulo { get; set; }
}
  • Thank you, André, for your reply. I have already done this, including tried many other things, doing what you asked occurs error: "One or more validation errors Were Detected During model Generation: r n nViatura_Modulo_Source: Multiplicity is not Valid in Role 'Viatura_modulo_source' in Relationship 'Viatura_modulo'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'

  • Moduloid must be long and not nullable.

  • public virtual Vehicle Vehicle { get; Sep; } => public virtual Ilist<Vehicle> Vehicles { get; Sep; }

Browser other questions tagged

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