0

Even though some will argue that "if it doesn't have a primary key, it's not a table", sometimes You have to accept the requirements of customer.

I have a table without primary keys and I use Entity framework Code first from existing database to access data. Entity framework has generated a model and set [Key] and [Column] annotation to all NOT NULL columns. Here is a simplified situation with 3 mandatory (NOT NULL) and one optional (NULL) column:

    [Key]
    [Column(Order = 0)]
    public int Manadatory1 { get; set; }

    public string Optional { get; set; }

    [Key]
    [Column(Order = 1)]
    public short Manadatory2 { get; set; }

    [Key]
    [Column(Order = 2)]
    public string Manadatory3 { get; set; }

When I have two rows in the table, where there is the only difference in the "Optional" column, Entity framework returns the same value for both, because it considers them to be one, because the [Key] values are equal.

I was not able to find any solution for this without changing anything in the database. I tried to change the configuration of the model and to set the optional column as a key, but it is not allowed to have NULL in the key.

Is there any solution in entity framework for such a problem without making any change in the database?

EDIT: There are some similar questions in the StackOverflow, but there is the accepted question with changing the datamodel. The other solution would be not to use entity framework. The aim of this question is to find if such a way exists also using EF.

Thank You very much, Tomáš

Tomas
  • 369
  • 1
  • 8
  • 13

0 Answers0