ORMConcurrenctException on Save with DataAdapter

Posts   
 
    
peschkaj
User
Posts: 30
Joined: 21-Sep-2006
# Posted on: 23-Oct-2007 18:45:43   

I'm using LLBLGen 2.5, SQL Server 2005 (9.0.1399), VS2005, and .NET 2.0.

While attempting to save a newly created entity, the code throws an ORMConcurrencyException.

After reading this thread: http://llblgen.com/TinyForum/Messages.aspx?ThreadID=10121 I looked into the connection properties and made sure that NOCOUNT is set to OFF.

The code in question is:

UserRoleEntity _ure = new UserRoleEntity();
_ure.IsNew = false;
_ure.User = _user;
_ure.Role = _role;
_ure.Organization = null;

if (!_adapter.FetchEntityUsingUniqueConstraint(_ure, _ure.ConstructFilterForUCUserIdRoleIdOrganizationId()))
{
    _ure = new UserRoleEntity();
    _ure.UserRoleId = Guid.NewGuid();
    _ure.User = _user;
    _ure.Role = _role;
    _ure.Organization = null;

    try
    {
        _adapter.SaveEntity(_ure);
    }
    catch (Exception ex)
    {
        throw new Exception(ex.Message, ex);
    }
}

I ran SQL Profiler against the SaveEntity call, and two statements are run:


exec sp_executesql N'INSERT INTO [hisd_new].[dbo].[UserRole] ([UserRoleID], [UserID], [RoleID])  VALUES (@UserRoleId, @UserId, @RoleId)',
N'@UserRoleId uniqueidentifier,@UserId uniqueidentifier,@RoleId uniqueidentifier',
@UserRoleId='1E50E42F-E66B-4D8E-B0BA-A4AB19E110C9',
@UserId='4AA605F8-B0DE-49EA-AE2D-5443A980BAF9',
@RoleId='601D108C-E8E6-48D3-A10F-C4F53CC6D39D'

exec sp_executesql N'UPDATE [hisd_new].[dbo].[UserRole] SET [UserID]=@UserId,[RoleID]=@RoleId WHERE ( [hisd_new].[dbo].[UserRole].[UserRoleID] IS NULL)',
N'@UserId uniqueidentifier,@RoleId uniqueidentifier',
@UserId='4AA605F8-B0DE-49EA-AE2D-5443A980BAF9',
@RoleId='601D108C-E8E6-48D3-A10F-C4F53CC6D39D'

Thanks in advance for any help on this, I've stepped through this a number of times, and I'm at a bit of a loss.

Jeremiah

peschkaj
User
Posts: 30
Joined: 21-Sep-2006
# Posted on: 23-Oct-2007 21:40:07   

The full text of the exception is:


failed: System.Exception : During a save action an entity's update action failed. The entity which failed is enclosed.
  ----> SD.LLBLGen.Pro.ORMSupportClasses.ORMConcurrencyException : During a save action an entity's update action failed. The entity which failed is enclosed.
    C:\Projects\BattelleForKids\com.hmbnet.BattelleForKids.ServiceLayer\BfkRoleProvider.cs(177,0): at com.hmbnet.BattelleForKids.ServiceLayer.BfkRoleProvider.AddUsersToRoles(String[] userNames, String[] roleNames)
    C:\Projects\BattelleForKids\UnitTests\BfkRoleProviderTest.cs(156,0): at com.hmbnet.BattelleForKids.UnitTests.BfkRoleProviderTest.AddNewUserToRole()
    --ORMConcurrencyException
    at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.PersistQueue(List`1 queueToPersist, Boolean insertActions, Int32& totalAmountSaved)
    at SD.LLBLGen.Pro.ORMSupportClasses.UnitOfWork2.Commit(IDataAccessAdapter adapterToUse, Boolean autoCommit)
    C:\Projects\BattelleForKids\com.hmbnet.BattelleForKids.ServiceLayer\BfkRoleProvider.cs(173,0): at com.hmbnet.BattelleForKids.ServiceLayer.BfkRoleProvider.AddUsersToRoles(String[] userNames, String[] roleNames)

peschkaj
User
Posts: 30
Joined: 21-Sep-2006
# Posted on: 23-Oct-2007 21:52:11   

When I changed the save code to use the FK fields instead of the referenced objects, everything worked correctly.

Now the code in question reads:

if (!_adapter.FetchEntityUsingUniqueConstraint(_ure, _ure.ConstructFilterForUCUserIdRoleIdOrganizationId()))
{
    _ure = new UserRoleEntity();
    _ure.UserRoleId = Guid.NewGuid();
    _ure.UserId = _user.UserId;
    _ure.RoleId = _role.RoleId;
    _ure.OrganizationId = null;

    try
    {
        //_adapter.SaveEntity(_ure);
        UnitOfWork2 _uow = new UnitOfWork2();

        _uow.AddForSave(_ure);

        _uow.Commit(_adapter, true);
    }
    catch (Exception ex)
    {
        throw new Exception(ex.Message, ex);
    }
}
Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 24-Oct-2007 12:37:15   

When I changed the save code to use the FK fields instead of the referenced objects, everything worked correctly.

That's better.

Also since you are using SQL Server 2005, it's recommended that you make use of the NEWSEQUENTIALID() feature, along with setting the SqlServer 2005 compatibility mode.

Please refer to the LLBLGen Pro manual: Using the generated code -> Database specific features