none
Why does the Entity modeller insist on a unique key RRS feed

  • Question

  • I came across the problem whereby the lack of a unique key caused the same record to be repeated.

    This was in a subset of a dataset where no unique key is available. There is in the full set of columns, but this causes repeats in the subset required by the users - which they don't want.

    The only answer I've found for this is to get the entity modeller to return the entire set and then using Linq to make a distinct query on the subset - but this breaks the in-house rule that business rules should be on the database.

    So in this instance the restriction that there is a unique key actually becomes a problem.

     

    • Moved by VMazurModerator Thursday, June 17, 2010 10:39 AM (From:ADO.NET Managed Providers)
    Thursday, June 17, 2010 8:31 AM

Answers

  • Not sure to understand the problem. Are you sure you don't have duplicates server side in your DB ? Generally EF needs a unique key to be able to uniquely identify each and every entity so that it can be updated.

    Either you have duplicates in your DB and Linq just exposes this. Or you don't have duplicates and then you have an operation that causes this (as you could with SQL by misuing a join or something similar).

    Let's say you just have a table T1 with columns A and B with no unique keys and few records and perhaps a T2 table with column A and values matching in table T1.

    What is the LINQ request you are doing against this table and that would produce duplicates ?

     

     

    Thursday, June 17, 2010 4:58 PM

All replies

  • Not sure to understand the problem. Are you sure you don't have duplicates server side in your DB ? Generally EF needs a unique key to be able to uniquely identify each and every entity so that it can be updated.

    Either you have duplicates in your DB and Linq just exposes this. Or you don't have duplicates and then you have an operation that causes this (as you could with SQL by misuing a join or something similar).

    Let's say you just have a table T1 with columns A and B with no unique keys and few records and perhaps a T2 table with column A and values matching in table T1.

    What is the LINQ request you are doing against this table and that would produce duplicates ?

     

     

    Thursday, June 17, 2010 4:58 PM
  • You have answered my question and this seems reasonable when reading sets that may need to change, but not so much when the set is read only - as in this case (where all I want is a list of records, I'm not even using the records to jump to a detail page).

     

    I do not have duplicate records, but the 'key' chosen by the entity modeller is a duplicate, since it has picked the ProductId as a key for this view, and the query shows the list of pharmaceutical drugs and how they interact with other drugs (as in say Ibuprofen and Paracetamol or Aspirin). So in showing Ibuprofen - I want a list of drugs that interact with it.

     

    The linq query is a straight selection - at my current site we do all our queries behind stored procedures and as such the Linq query need do nothing more complicated than to select the data.

     

     

     

    Friday, June 18, 2010 8:28 AM