Founder
Founder

Reputation: 626

Stored Procedure without transaction in Entity Framework

I'm calling a stored procedure in Entity Framework 6 that can create Databases and tables if necessary. It is throwing the error;

Message "CREATE DATABASE statement not allowed within multi-statement transaction.\r\nALTER DATABASE statement not allowed within multi-statement transaction.\r\nDatabase 'CoreSnapshotJS3' does not exist. Make sure that the name is entered correctly." string

I do not want it in a transaction, and have used this to supress the transaction

using (var transation = new TransactionScope(TransactionScopeOption.Suppress))
{
    return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("spCreateSnapshotFromQueue", snapshotQueueIDParameter);    
}

It still throws an error.

How do I stop automatic transactions?

Upvotes: 7

Views: 5484

Answers (1)

Founder
Founder

Reputation: 626

I found a way:

var snapshotQueueIDParameter = new SqlParameter("SnapshotQueueID", entityId);
return _db.Database.ExecuteSqlCommand(TransactionalBehavior.DoNotEnsureTransaction,
        "EXEC spCreateSnapshotFromQueue @SnapshotQueueID", snapshotQueueIDParameter);

Upvotes: 26

Related Questions