Error on CancelNew

4 posts, 0 answers
  1. Heiko
    Heiko avatar
    124 posts
    Member since:
    Oct 2012

    Posted 10 Sep 2012 Link to this post

    In my application using QDSCV I simply add a new entity via "AddNew(...)". If the user cancels this adding operation I call "CancelNew()" which leads to an error.
    I investigated the error a little bit and found out that in "DomainServiceCollection" the "OnCollectionChanged" event is triggered trying to remove the item from the EntitySet, But: the item has never been added to the underlying EntitySet. When adding items the same "OnCollectionChanged" event is called and it tries to add the new item to the EntitySet. Unfortunately it is checking for "EntityState.Detached" instead of "EntityState.New" which is the EntityState the newly added item is in. Thus it is NOT in the EntitySet and the remove operation later on crashes.

    foreach (TEntity item in e.NewItems)
    {
        if (item.EntityState == EntityState.Detached)
        {
            entitySet.Add(item);
        }
        this.totalItemCount++;
    }

    Also in my opinion the line "totalItemCount++" belongs in between the brackets under the Add-statement - why should the totalItemCount be increased if not item has been added to the entitySet?

    Regards
    Neils
  2. Nedyalko Nikolov
    Admin
    Nedyalko Nikolov avatar
    871 posts

    Posted 13 Sep 2012 Link to this post

    Hi,

    I've tried to reproduce the reported exception, unfortunately to no avail.
    I'm attaching my sample application for a reference. Could you please tell me how to simulate the error?
    Thank you in advance.

    P.S. The sample application requires SQLEXPRESS with Northwind installed (or you could change the connection string, but please use Northwind or AdventureWork databases or you will have to send me your database).

    Kind regards,
    Nedyalko Nikolov
    the Telerik team

    Time to cast your vote for Telerik! Tell DevPro Connections and Windows IT Pro why Telerik is your choice. Telerik is nominated in a total of 25 categories.

  3. DevCraft banner
  4. Heiko
    Heiko avatar
    124 posts
    Member since:
    Oct 2012

    Posted 17 Sep 2012 Link to this post

    Good morning!

    I checked your example and indeed the entities state is "Detached" so no problem occurs. Here is my code:

    EmailAddress email = new EmailAddress();
    email.Person = this.CurrentPerson;
    email.MailAddressType = MailAddressTypes.FirstOrDefault();
    email.PreferredFlag = (!Mails.Any());
    Mails.AddNew(email);

    As soon as I set the MailAddressType the entities state changes from "Detached" to "New" so this causes the error later on (where the entity is not added to the internal EntitySet). WCF RIA Services has one more state "New" which the normal System.Data Enumeration has not. I can't tell you why the state changes but it seems this is how WCF RIA Services is working.

    System.ServiceModel.DomainServices.Client.EntityState
        Deleted: Deleted
        Detached: Detached
        Modified: Modified
        New: New
        Unmodified: Unmodified


    Regards
    Neils
  5. Nedyalko Nikolov
    Admin
    Nedyalko Nikolov avatar
    871 posts

    Posted 18 Sep 2012 Link to this post

    Hi,

    Could you send me a sample application which I can debug on my side in order to see what is going on?
    Thank you in advance.

    Greetings,
    Nedyalko Nikolov
    the Telerik team

    Time to cast your vote for Telerik! Tell DevPro Connections and Windows IT Pro why Telerik is your choice. Telerik is nominated in a total of 25 categories.

Back to Top