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();
        }
    }
Eitan K
  • 837
  • 1
  • 17
  • 39

1 Answers1

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.

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
  • Thanks for the advice! I made the changes you've said but I still get the same error (see my updated question). I have kept the transactionscope because if the insert to InvestigatorGroupUser fails I want the insert into InvestigatorGroup to fail as well – Eitan K Apr 22 '16 at 12:48
  • The point is that you don't need the TransactionScope. `SaveChanges` does *all inserts* in one transaction it start and commits itself. – Gert Arnold Apr 22 '16 at 12:51
  • 1
    While @GertArnold is correct and the transaction is unnecessary in this case, I believe http://stackoverflow.com/questions/10130767/the-transaction-manager-has-disabled-its-support-for-remote-network-transactions will explain how to fix this error if you encounter it in a context where this is required. – dmeglio Apr 22 '16 at 12:54
  • I removed the TransactionScope and it now works thanks! Just out of curiosity, how come I get an error when I keep the TransactionScope? I don't believe it is due to DTC as I have another operation that uses TransactionScope and does not generate an error – Eitan K Apr 22 '16 at 13:02
  • I don't know. Your question didn't show where the TransactionScope started and what else happened within its scope. Only wrapping the code you show in a TransactionScope shouldn't make a difference (although it wouldn't be necessary). – Gert Arnold Apr 22 '16 at 13:04
  • @EitanK it depends on what that other transactionscope is doing. DTC may be enabled, but is DTC network access enabled? Perhaps the other scope doesn't need the network access. – dmeglio Apr 22 '16 at 13:10