Insert into multiple tables using WCF Transactions

0

I am trying to add an entry into a table and use the primary key of that added entry to create an additional entry into another table.

The error I am getting is

The transaction manager has disabled its support for remote/network transactions. (Exception from HRESULT: 0x8004D024)

I believe this is caused by creating multiple connections within a single TransactionScope, but I am doing everything within one context / using statement, so I do not believe that I should be receiving this error.

Service

    [OperationBehavior(TransactionScopeRequired = true)]
    public void CreateGroup(NewGroupData data)
    {
        var groupRepo = _GroupRepo ?? new InvestigatorGroupRepository();
        groupRepo.CreateGroup(data.UserId, data.InvestigatorGroupName, data.HasGameAssignment, data.InstitutionId);

    }

Repository

   public void CreateGroup(string userId, string investigatorGroupName, bool hasGameAssignment, int institutionId)
    {
        using (var context = new GameDbContext())
        {
            var newGroup = new InvestigatorGroup()
            {
                InvestigatorGroupName = investigatorGroupName,
                HasGameAssignment = hasGameAssignment,
                InstitutionId = institutionId,
                IsTrashed = false
            };

            int institutionUserId =
                context.InstitutionUsers.Where(
                    iu => !iu.IsTrashed && iu.APUser.UserId == userId && iu.InstitutionId == institutionId).Select(iu => iu.InstitutionUserId).Single();

            var newGroupUser = new InvestigatorGroupUser()
            {
                InstitutionUserId = institutionUserId,
                InvestigatorGroup = newGroup,
                CreationDate = DateTime.Now
            };
            context.InvestigatorGroupUsers.Add(newGroupUser);

            context.SaveChanges();
        }
    }
c#
asp.net
entity-framework
wcf
transactions
asked on Stack Overflow Apr 21, 2016 by Eitan K • edited Apr 22, 2016 by Eitan K

1 Answer

1

You start with a wrong assumption.

The line...

int newGroupId = context.InvestigatorGroups.Add(newGroup).InvestigatorGroupId;

...will always assign 0 to newGroupId. The Add method only marks the entity for insert, but doesn't actually insert it. Only SaveChanges writes data to the database, not any other method in Entity Framework.

So the assignment...

InvestigatorGroupId = newGroupId,

...is faulty as well. You have to assign the new InvestigatorGroup to a navigation property in InvestigatorGroupUser:

InvestigatorGroup = newGroup,

Add this navigation property to InvestigatorGroupUser if you haven't got it yet.

If you have that, it's enough to execute these lines:

context.InvestigatorGroupUsers.Add(newGroupUser);
context.SaveChanges();

No need to Add the newGroup object too, It will be added by adding newGroupUser.

So if you do that, the only transaction you need is the one that SaveChanges uses internally by default. For the code you show, you don't need a TransactionScope. If this is part of a greater WCF transaction the story may be different, but I think at least you needed some misconceptions to be straightened out.

answered on Stack Overflow Apr 21, 2016 by Gert Arnold

User contributions licensed under CC BY-SA 3.0