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.
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.
– Marcos Brinner
Foreignkey("Module")] is a foreign Moduloid key and not a Car ID.
– Andre Mesquita
No Mark, I did it separately, first I did it only with Data Annotation and then only with Fluent API.
– Alberto Luiz Schaub
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.
– Alberto Luiz Schaub
@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.
– Thiago Araújo