none
EF is trying to insert new child record before new parent record during SaveChanges RRS feed

  • Question

  • I am using database first and have a parent entity that has a collection of child entities. When I am adding both at the same time I am getting a FK constraint in the DB and when I log EF's commands I see that it is sending the sql command to insert the child record first in the UOW and of course it blows up.

    The code is first adding the Ticket using dbSet.Add and then it adds the StockTicket to Ticket.StockTickets. It then calls SaveChanges()

    I have also tried to add both Ticket and StockTicket to their respective dbSets and then manually add the StockTicket to Ticket.StockTickets and the Ticket to StockTicket.Ticket, but that doesn't work either

    Nothing works and I haven't run into this before; I have worked with EF quite a bit.

    public partial class Ticket 
    {
        public Ticket()
        {
            this.StockTickets = new HashSet<StockTicket>();         
        }
    
        public long TicketID { get; set; }
        ...
    
        public virtual ICollection<StockTicket> StockTickets { get; set; }
    }
    
    public partial class StockTicket
    {
        public long StockTicketID { get; set; }
        ...
    
        public virtual Ticket Ticket { get; set; }
    }
    
    // base repository methods
    protected void Insert(TEntity entity)
    {
        if (entity == null)
        {
            throw new ArgumentNullException("entity", "The entity argument is null.");
        }
    
        var entry = _context.Entry(entity);
        entry.Property("isActive").CurrentValue = true;
        _dbSet.Add(entity);
    }
    
    public void Save()
    {
        _context.SaveChanges();
    }

    Calling code in my service:

    ticket.ticketID = GetNextSequence();
    _repository.Insert(ticket);
    ticket.StockTickets.Add(stockTicket);
    _repository.Save();

    UPDATE:

    The problem ended up being due to the something in the edmx being corrupt. Creating a new one from scratch fixed all of the issues!

    thanks all!



    • Edited by TS46235 Friday, May 29, 2015 8:58 PM FIXED
    Saturday, May 23, 2015 12:15 PM

Answers

  • Hello TS46235,

    It is glad to see you get a solution and i would split your answer to be a separate reply so that this would make more sense for this issue:

    Solution provided by OP for this issue:

    The problem ended up being due to the something in the edmx being corrupt. Creating a new one from scratch fixed all of the issues!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, June 1, 2015 3:42 AM
    Moderator

All replies

  • I am using database first and have a parent entity that has a collection of child entities. When I am adding both at the same time I am getting a FK constraint in the DB and when I log EF's commands I see that it is sending the sql command to insert the child record first in the UOW and of course it blows up.

    You can co back to the DB first and configure the model at creation to expose all primary and forigen key properties in the entities.

    You insert the parent entity first, and upon the insertion of the object, the primary key is populated back to the entity's primary-key property. You can then take the primary-key of the parent object and populate the foreign-key property of each child object inserted. That is assuming you are using the Identity as the primary-key on the table records.

    I don't know what you are trying to do here, but if the parent object is new and all the child objects within the parent entity are new in the collection in the parent, then EF should do it with no problem upon insertion of the objects by itself.  Maybe you should try some other way before leaping into the UOW pattern, and see the insert work properly.

    IIBTW

    Saturday, May 23, 2015 6:37 PM
  • I don't know what you mean by 'expose all primary & FK properties'.  I had the FKs set in db before I generated the model and the relationships do exist in it.

    If I look at the edmx with a text editor I see this:

    Errors Found During Generation:
    warning 6030: The relationship 'FK_STOCKTICKET_TICKET' uses the set of foreign keys '{ticketID,MID}' that are partially contained in the set of primary keys '{stockTicketID,merchantID}' of the table 'StockTicket'.  The set of foreign keys must be fully contained in the set of primary keys, or fully not contained in the set of primary keys to be mapped to a model.

    This FK is a composite FK (both tables' PK are composite), so I'm thinking this could be a problem.

    I have tried to explicitly set the FK properties' values before and after adding the Ticket to the context without any change. While inspecting the context I can drill down into the RelationshipManager, and inspecting the relationship for this FK it says IsLoaded = false...this may be correct though since they are both inserts.

    Also for the each entityEntry's relationship pointing to the other entity, it is using an System.Data.Entity.Core.Objects.Internal.System.Data.Entity.Core.Objects.Internal.EntityWrapperWithoutRelationships as the Owner property. When I query a Ticket object, that relationship is an EntityWrapperWithRelationships. Not sure if that means anything or not though.

     
    Monday, May 25, 2015 7:51 PM
  • I don't know what you mean by 'expose all primary & FK properties'.  I had the FKs set in db before I generated the model and the relationships do exist in it.

    Yeah that's true. But in creating a DB first model, which is from an existing DB schema, it gives you the option to expose the primary & FK properties in the EF entity so that they can be addressed and controlled by the developer in code. If you didn't select the option with EF 4 and above, then the properties are not exposed in the entities.

    Monday, May 25, 2015 8:05 PM
  • Hello TS46235,

    It is glad to see you get a solution and i would split your answer to be a separate reply so that this would make more sense for this issue:

    Solution provided by OP for this issue:

    The problem ended up being due to the something in the edmx being corrupt. Creating a new one from scratch fixed all of the issues!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, June 1, 2015 3:42 AM
    Moderator
  • thanks, but I was wondering why it took so long for an MS staff to get back to me. Is there still a 48 hr turn around time?
    Tuesday, June 2, 2015 4:29 PM
  • thanks, but I was wondering why it took so long for an MS staff to get back to me. Is there still a 48 hr turn around time?

    What MS staff? If you are talking about this forum, then all of this is voluntary and no one is working for or being paid by MS.
    Tuesday, June 2, 2015 5:08 PM
  • This is not an MSDN managed newsgroup? With an MSDN subscription you get "priority support in MSDN forums - Get expert answers to your technical questions within two business days"
    Tuesday, June 2, 2015 5:49 PM
  • This is not an MSDN managed newsgroup? With an MSDN subscription you get "priority support in MSDN forums - Get expert answers to your technical questions within two business days"

    I don't know where you got that, but it is not correct. No one in these MSDN forums is getting paid to reply to your posts  No one has any obligation to reply to your posts in these forums. I got an MSDN subsection too that my company pays. If you are getting support that's paid for, then you are going to be in some kind of chat, email or by phone, and they are going to be asking how are you paying for this out the gate. :)

    Joe Blow on the moon that doesn't have a dime to his name and paid for nothing concerning a MSDN anything can create  a MSDN login, come to the MSDN forums and start posting for answers and do replies to questions asked too. :)

    There was a poster in the MSDN  C# forum that had the same assumption you have about the MSDN forums. I had to set him straight too.  

    If someone is getting PAID in these forums, then I want my piece of the action too! :)

    Capiche?

    Wednesday, June 3, 2015 2:29 AM
  • Sorry to say, but you are wrong. I have had a subscription for many years and have gotten priority support for years. If you log into your msdn subscription, right now, you will see on the home page on the right, under where it lists 'Subscription benefits' see what I was talking about. I recall that all subscriptions get this support so you should see it. This is only a subset of the benefits, but see 2nd one:

    

    Wednesday, June 3, 2015 1:46 PM
  • Fred Bao is the MS moderator for this forum. He did come in and address my post after all.
    Wednesday, June 3, 2015 2:02 PM
  • https://msdn.microsoft.com/en-us/subscriptions/aa974230.aspx

    Yeah, I wouldn't put a whole lot into some kind of priority support in these forums. :) And I am not using my MSDN subscription  login either. If I want priorty support, I am on the phone talking directly to MS personel.

    Wednesday, June 3, 2015 5:21 PM