none
Required is Treated as ConcurrencyCheck RRS feed

  • Question

  • Hello again,

    When deleting an entity with a [Required] association, this association's value is also sent in the DELETE SQL statement, as if it were a [ConcurrencyCheck]:

    exec sp_executesql N'delete [dbo].[Project] where ((([ProjectId] = @0) and ([Customer_CustomerId] = @1))',N'@0 int,@1 int',@0=1,@1=1

    Why was this decision taken? It seems to me that it doesn't make sense. This effectively prevents deleting detached entities by id, when we don't know the keys of all the required associations.

    Thanks,

    RP

    (Cross posted from Stacknbsp;http://stackoverflow.com/questions/18553273/required-is-treated-as-concurrencycheck)

    Saturday, August 31, 2013 10:16 PM

Answers

  • Hello Ricardo Peres,

    EF uses this type of association when the foreign key for the relationship is not mapped to a property of an entity.

    Without going into too much detail about IAs I generally try to avoid them because they can cause several complications.

    One of these complications is that sometimes the FK value is sued as if it were a concurrency token, as you are seeing here.

    The best way to get around this is to add a foreign key property to the entity.

    Just wanted to add that in EF6 we can customize the DML that gets generated via the new interception capabilities. In this particular case we could rewrite the update command tree to remove the IA FK checks.

    Regards.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Friday, September 6, 2013 9:41 AM
    Moderator

All replies

  • If everything is in an attached state, then this is no problem. If objects are detached in this situation, then you have delete them manually starting at the children objects and working your way back to the parent. You would have to do an Attach on the object and delete it by its key. Forget about EF being there, because you have to know the structure and table associations of the underlying database you are working with. It's your job to know it. 

    http://blogs.msdn.com/b/alexj/archive/2009/03/27/tip-9-deleting-an-object-without-retrieving-it.aspx

    http://blogs.msdn.com/b/alexj/archive/2009/03/26/index-of-tips.aspx

    Sunday, September 1, 2013 2:34 PM
  • Thanks for your reply, but that doesn't answer my question: if you try the solution in any of these posts, you will see this problem exactly, at least with Code First!

    I am trying something like this:

    ctx.Entry(new Product{ ProductId = 1 }).State = EntityState.Deleted;

    ctx.SaveChanges();  //exception is thrown here due to the missing Customer property

    RP


    Sunday, September 1, 2013 10:01 PM
  • Hello Ricardo Peres,

    EF uses this type of association when the foreign key for the relationship is not mapped to a property of an entity.

    Without going into too much detail about IAs I generally try to avoid them because they can cause several complications.

    One of these complications is that sometimes the FK value is sued as if it were a concurrency token, as you are seeing here.

    The best way to get around this is to add a foreign key property to the entity.

    Just wanted to add that in EF6 we can customize the DML that gets generated via the new interception capabilities. In this particular case we could rewrite the update command tree to remove the IA FK checks.

    Regards.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Friday, September 6, 2013 9:41 AM
    Moderator