I am developing a C# application with .net version 4.5.2 and an Oracle DB.
The problem I have is that I can't get Entity Framework configured right to the database. The database contains a unique keys over 4 columns, but 2 of them are nullable.
[Key, Column("GEBRUIKER", Order = 0)]
public string User { get; set; }
[Key, Column("EL1", Order = 1)]
public string Element1 { get; set; }
[Key, Column("EL2", Order = 2)]
public short? Element2 { get; set; }
[Key, Column("EL3", Order = 3)]
public short? Element3 { get; set; }
When I try to get the values through this code from the database I get a null reference exception, because element 2 or 3 is empty.
When I remove the Keys from element 2 and 3, I wont get the right data, because when element 1 is the same at 2 rows, the second row will cache element 2 and 3.
Question: How can I handle these nullable unique keys?
Added extra information:
Well, this is a part of the create script of the database:
CREATE TABLE USERS
(
GEBRUIKER VARCHAR2(3 BYTE) NOT NULL,
EL1 VARCHAR2(6 BYTE) NOT NULL,
EL2 NUMBER,
EL3 NUMBER
)
CREATE UNIQUE INDEX USERS_UK ON USERS
(GEBRUIKER, EL1, EL2, EL3)
ALTER TABLE USERS ADD (
CONSTRAINT USERS_UK
UNIQUE (GEBRUIKER, EL1, EL2, EL3)
USING INDEX USERS_UK
ENABLE VALIDATE);
It is not possible to make any changes to the structure or data, because there are multiple applications who use this database. Also there are already EL2 and EL3 rows with the value 0.
Example data:
{'USER1','A','A','C'}
{'USER1','A','B','B'}
{'USER1','B','A','C'}
When I do a linq query to select USER1 AND EL1 = A I will get the next result:
{'USER1','A','A','C'}
{'USER1','A','A','C'}
instead of:
{'USER1','A','A','C'}
{'USER1','A','B','B'}