Troublesome situation!

Posts   
 
    
Jeff M
User
Posts: 250
Joined: 04-Aug-2004
# Posted on: 04-Jun-2006 19:40:07   

Hi all.

I don't know if this is a bug or what, but after two days of trying to figure it out, I decided to post the problem here. Using ver 2.0, net 2, latest ver of beta (June 1), adapter, remoting, SqlServer

Since the framework that supports this code sequence is rather complex, I'll provide a pseudocode to start with. Please bear with me. Here goes:

public void CreateRecord()
{
    ParentEntity parent = new ParentEntity()
    ChildEntity child1 = parent.ChildrenEntities.Add(new ChildEntity())
    ChildEntity child2 = parent.ChildrenEntities.Add(new ChildEntity())
    ChildEntity child3 = parent.ChildrenEntities.Add(new ChildEntity())   
}

Ok, everything's fine so far. ChildEntity contains a unique non-PK field called "UniqueString". Uniqueness is maintained by a nonclustered SQlServer index on the child table.

public void PopulateChildrenFields()
{
     child1.UniqueString = "X"  <--- same as child3
     child2.UniqueString = "Y"
     child3.UniqueString = "X"  <--- same as child1
     adapter.Save(parent) <--- Using remote adapter
} 

As expected, this call triggers a SqlServer error since two of the children entities have the same ChildCodeString.

public void RemoveChildEntity()
{
    ShowGridControl() <--- three children entities are shown
    parent.ChildrenEntities.Remove(child1)  <-- remove one of the offending entities
    ShowGridControl() <--- now, only two children entities are shown
    adapter.Save(parent)
}

Can't save! Unique field violation!

What? I get the same error message! Says that there STILL is violation on the unique field... but why is that?

public void ChangeChildField()
{
     child3.UniqueString = "X1"
    adapter.Save(parent) with refetch
    ShowGridControl() <--- Huh?  All three children are listed again!
}

What gives? This save worked because there is no longer a violation on UniqueString. But why are there STILL three children records? Why wasn't child1 removed, per the code? The GridControl showed that the record ware removed. I even checked (through the debugger) that parent only had two children entities after the remove.

Has anyone else experienced this? Any help please?

Thanks!

Jeff

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39928
Joined: 17-Aug-2003
# Posted on: 05-Jun-2006 11:33:09   

Jeff wrote:

Hi all.

I don't know if this is a bug or what, but after two days of trying to figure it out, I decided to post the problem here. Using ver 2.0, net 2, latest ver of beta (June 1), adapter, remoting, SqlServer

Since the framework that supports this code sequence is rather complex, I'll provide a pseudocode to start with. Please bear with me. Here goes:

public void CreateRecord()
{
    ParentEntity parent = new ParentEntity()
    ChildEntity child1 = parent.ChildrenEntities.Add(new ChildEntity())
    ChildEntity child2 = parent.ChildrenEntities.Add(new ChildEntity())
    ChildEntity child3 = parent.ChildrenEntities.Add(new ChildEntity())   
}

Ok, everything's fine so far.

No simple_smile EntityCollection<T> doesn't have an Add method which returns the value added. Is that Add() a method you added yourself, or is it because you wrote it in pseudocode so in real code it looks different? It might be important though, so please, if possible, post a real snippet.

ChildEntity contains a unique non-PK field called "UniqueString". Uniqueness is maintained by a nonclustered SQlServer index on the child table.

So it's not picked up as a UC by LLBLGen Pro, because it's an index, not a constraint, OK.

public void PopulateChildrenFields()
{
     child1.UniqueString = "X"  <--- same as child3
     child2.UniqueString = "Y"
     child3.UniqueString = "X"  <--- same as child1
     adapter.Save(parent) <--- Using remote adapter
} 

As expected, this call triggers a SqlServer error since two of the children entities have the same ChildCodeString.

the remote adapter, I pressume you call a service which uses an adapter internally? As DataAccessAdapter isn't remotable. It might be handy to have that save code here as well.

public void RemoveChildEntity()
{
    ShowGridControl() <--- three children entities are shown
    parent.ChildrenEntities.Remove(child1)  <-- remove one of the offending entities
    ShowGridControl() <--- now, only two children entities are shown
    adapter.Save(parent)
}

Can't save! Unique field violation!

What's the count of parent.CHildrenEntities ? Is child1 having relations with other entities? and are those entities related to child2/3 ?

What? I get the same error message! Says that there STILL is violation on the unique field... but why is that?

public void ChangeChildField()
{
     child3.UniqueString = "X1"
    adapter.Save(parent) with refetch
    ShowGridControl() <--- Huh?  All three children are listed again!
}

What gives? This save worked because there is no longer a violation on UniqueString. But why are there STILL three children records? Why wasn't child1 removed, per the code? The GridControl showed that the record ware removed. I even checked (through the debugger) that parent only had two children entities after the remove.

Has anyone else experienced this? Any help please?

Please investigate if child1 has a relation with an entity Foo which is related to child2 for example. If you then remove child1 from ChildEntities, it's still in the object graph because it's related to Child2.Foo and when Child2 is saved, Child1 is thus saved as well.

Frans Bouma | Lead developer LLBLGen Pro