not rocket science!

Posts   
 
    
hchattaway
User
Posts: 38
Joined: 06-Apr-2006
# Posted on: 10-Aug-2010 05:01:27   

Hello!

I am back to using LLBLGen after a short hiatus. I am simply trying to do a "SaveEntity()" on one of my tables. I create a new entity, set the PK, set IsNew to false, set a field to update and then pass the object to "SaveEntity()". It always ends up inserting a record instead of updating the existing one. The PK definitely exists and in the debugger, the base class for the enitity object shows the correct table and PK field and value...

What could be making this happen? I really don't think I am missing anything..

Thanks Harold

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 10-Aug-2010 05:35:47   

Hi Harold,

What LLBLGen version are you using? Is the PK set as identity? Please show us your code snippet.

David Elizondo | LLBLGen Support Team
hchattaway
User
Posts: 38
Joined: 06-Apr-2006
# Posted on: 11-Aug-2010 04:59:59   

Hi,

I just got the latest of 3.0, the Aug 5th release, regenerated and still same issue... Yes, the PK field is set.

With a bit more tracing, i've seen more weird stuff:

Here is the TestMethod:


     [TestMethod]
        public void WCFUpdateIssue()
        {

            UpdateIssueDTORequest request = new UpdateIssueDTORequest();

            UserProxy clientProxy = new UserProxy();

            IssueEntity newIssue = new IssueEntity();

            newIssue.IssuePk = 1012;
            newIssue.IsNew = false;
            newIssue.Title = "Should be updated!";

            request.Issue = newIssue;
                        
            string result = clientProxy.UpdateIssue(request);

            Assert.IsTrue(result == "Should be updated!");
            
        }

At the point the clientProxy.UpdateIssue() method is called, all of the values are set correctly. The Request object is a data transfer object (DTO) that is just a public field of type "IssueEntity". I am creating a new instance of that, populating the fields and assigning it to the DTO and passing it along.

The next stop is a business logic layer. At this point, all fields are correct also. It then passes the same object to the data access layer.


 public string UpdateIssue(UpdateIssueDTORequest request)
        {

            using (DataAccessAdapter adapter = new DataAccessAdapter())
            {
                Debugger.Break();

                request.Issue.IsNew = false;

                adapter.SaveEntity(request.Issue);
                adapter.FetchEntity(request.Issue);
            } 
            
            return request.Issue.Title.Trim();
        }


As soon as execution enters this block, the IsNew property flips to "True" but the other properties retain their assigned values! Since IsNew is now True, the SaveEntity() call inserts a record instead of updates! But if I reset the Isnew to false as shown above, it correctly updates! really strange.....

And yes, since this is an "Update" method, i could always just set IsNew property just before calling SaveEntity(), but shouldn't have to I don't think.. why should that one property flip?

Any ideas?

Harold

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 11-Aug-2010 07:41:40   

Hi Harold,

In the Compact25 format, which is used by default in WCF etc., every bit we could avoid to pack into the xml is indeed not included. So it only stores the entity.Fields.State value, and not IsNew, as IsNew is related to that state. At deserialization, the state is set to the value in the XML and IsNew is set to true if the state is 'New'.

So, in your case the State is always new as you are creating a new entity (you didn't fetch it). You should set the state to EntityState.Fetched prior to sending the entity over the wire. 'IsNew' is a flag not used in this matter.

 [TestMethod]
        public void WCFUpdateIssue()
        {

            UpdateIssueDTORequest request = new UpdateIssueDTORequest();

            UserProxy clientProxy = new UserProxy();

            IssueEntity newIssue = new IssueEntity();

            newIssue.IssuePk = 1012;
            newIssue.Fields.State = EntityState.Fetched;
            newIssue.Title = "Should be updated!";

            request.Issue = newIssue;
                        
            string result = clientProxy.UpdateIssue(request);

            Assert.IsTrue(result == "Should be updated!");
            
        }
David Elizondo | LLBLGen Support Team
hchattaway
User
Posts: 38
Joined: 06-Apr-2006
# Posted on: 12-Aug-2010 05:00:45   

ok, the entitystate.Fetched worked! Thanks very much!

harold

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 12-Aug-2010 07:40:54   

wink Nice

David Elizondo | LLBLGen Support Team