I have a serious problem with the Entity Framework .
I have two tables: Módulos
and Viaturas
, both with the name of the primary key ID ( Database First
).
A vehicle can have a Module, but it can also be null, the association is not mandatory, however I have an FK in the Viaturas
Table for 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 many ways, always without success in Entity.
Using only Data Annotations
...
Module Class:
[Table("MODULO")]
public partial class Modulo
{
[Key]
[Column("ID", TypeName = "NUMBER")]
public long ModuloID { get; set; }
public virtual Viatura Viatura { get; set; }
}
Car class:
[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 Fluent API only:
Module Config:
HasKey(t => t.ModuloID);
Property(b => b.ModuloID).HasColumnName("ID");
Car Settings:
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 the ID_MODULO column for the relationship in the Fluent API:
Car Settings:
HasKey(c => c.ViaturaID);
Property(p => p.ViaturaID).HasColumnName("ID");
HasRequired(h => h.Modulo).WithOptional().Map(m => m.MapKey("ID_MODULO"));
Module Config:
HasKey(t => t.ModuloID);
Property(b => b.ModuloID).HasColumnName("ID");
In this scenario, the relationship worked, but then Entity crashed, it now thinks that the column ID of the VIATURA table is called ViaturaID, and I said that the column name is ID.
{"ORA-00904: \" Extent2 \ ". \" Vehicle_ViaturaID \ ": identifier invalid "}
I'm using the most current version of Entity (6.2.0) in Dotnet Framework 4.5.
Thank you in advance.