I'm developing a Console Application using Entity Framework Core (7).
The application is divided into 3 different areas but the database is shared.
I created 3 different DbContext and now I need to perform a transaction between all of them.
So I need an atomic operation the save all the changes or nothing (rollback).
I know that in Entity Framework 6 there was a class called TransactionScope but I cant find an alternative in EF Core.
Using the following code:
public static void Main(string[] args)
{
var options = new DbContextOptionsBuilder<DbContext>()
.UseSqlServer(new SqlConnection("Server=x.x.x.x,1433;Database=test;user id=test;password=test;"))
.Options;
var cat = new Cat { Name = "C", Surname = "C", Age = 55 };
var dog = new Dog { Date = DateTime.Now, Code = 120, FriendId = cat.Id };
using (var context1 = new DogsContext(options))
{
using (var transaction = context1.Database.BeginTransaction())
{
try
{
context1.Dogs.Add(dog);
context1.SaveChanges();
using (var context2 = new CatsContext(options))
{
context2.Database.UseTransaction(transaction.GetDbTransaction());
context2.Cats.Add(cat);
}
transaction.Commit();
}
catch (Exception e)
{
Console.WriteLine(e);
transaction.Rollback();
}
}
}
}
I get the following error:
System.InvalidOperationException: ExecuteScalar requires the command to have a transaction when the connection assigned to the command is in a pending local transaction. The Transaction property of the command has not been initialized.
at System.Data.SqlClient.SqlCommand.ValidateCommand(Boolean async, String method)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite, String method)
at System.Data.SqlClient.SqlCommand.ExecuteScalar()
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, String executeMethod, IReadOnlyDictionary`2 parameterValues, Boolean openConnection, Boolean closeConnection)
at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.Exists()
at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
TransactionScope is not part of Entity Framework. Its part of the System.Transactions namespace. Additionally, TransactionScope is not the recommended approach for handling transactions with Entity Framework 6.x.
With Entity Framework Core you can share a transaction across multiple contexts for relational databases only. The contexts must share the same database connection.
More information here: https://learn.microsoft.com/en-us/ef/core/saving/transactions
Example (not tested):
using (var context1 = new YourContext())
{
using (var transaction = context1.Database.BeginTransaction())
{
try
{
// your transactional code
context1.SaveChanges();
using (var context2 = new YourContext())
{
context2.Database.UseTransaction(transaction.GetDbTransaction());
// your transactional code
}
// Commit transaction if all commands succeed, transaction will auto-rollback when disposed if either commands fails
transaction.Commit();
}
catch (Exception)
{
// handle exception
}
}
}
Related
we are trying to upgrade to CSLA 6.
now, we are getting a message:
"ConnectionManager is obsolete, use dependency injection ... use ApplicationContext.LocalContext"
for this code:
using (var ctx = ConnectionManager<OracleConnection>.GetManager("dbEndpoint", true))
We've tried this code snippet but all connections is NULL.
Could you please help us to correctly get Connection?
var services = new ServiceCollection();
services.AddCsla();
var provider = services.BuildServiceProvider();
DataPortalFactory = provider.GetRequiredService<IDataPortalFactory>();
var appContext = provider.GetRequiredService<Csla.ApplicationContext>();
var conn1 = appContext.LocalContext.GetValueOrNull("dbEndpoint");
var conn2 = appContext.LocalContext.GetValueOrNull("__db:default-dbEndpoint");
var conn3 = appContext.LocalContext["dbEndpoint"];
var conn4 = appContext.LocalContext["__db:default-dbEndpoint"];
another experiment:
....
var CONNECTION_ORACLE = new OracleConnection(ConfigurationManager.ConnectionStrings["dbEndpoint"].ConnectionString);
services.AddScoped<IDbConnection>(o => CONNECTION_ORACLE);
....
var provider = services.BuildServiceProvider();
...
var connectionResolved = provider.GetRequiredService<IDbConnection>();
appContext.LocalContext.Add("dbEndpoint", connectionResolved);
then connection is not null;
and inside of Factory is successfully resolved by DI:
public DocFactory(ApplicationContext appContext, IDbConnection connection) : base(
appContext)
{
_connection = connection;
}
then
[Fetch]
public Doc_Fetch(DocCriteria criteria)
{
bool cancel = false;
OnFetching(criteria, ref cancel);
if (cancel) return null;
Doc item = null;
OracleConnection connection = _connection as OracleConnection;
connection is Closed (but NOT null!!). it's possible to open it but if close it, somebody else consuming it will face with a problem or child objects also will face problem with closed connection.
so, making ConnectionManager as Obsolete may be not so obvious way to go. But ConnectionManager was very useful for counting open connection, supporting transactions etc
Could you please provide a workaround for it.
more attempts:
var connectionString =
ConfigurationManager.ConnectionStrings["dbEndpoint"].ConnectionString;
..
appContext.ClientContext.Add("DBConnectionString", connectionString );
...
Factory
using (var connection = new OracleConnection(ApplicationContext.ClientContext["DBConnectionString"].ToString()))
{
connection.Open();
Your DAL should require that a database connection be injected.
public class MyDal : IDisposable
{
public MyDal(OracleConnection connection)
{
Connection = connection;
}
private OracleConnection Connection { get; set; }
public MyData GetData()
{
// use Connection to get the data
return data;
}
public void Dispose()
{
Connection.Dispose();
}
}
Then in the app server startup code, register your DAL type(s) and also register your connection type.
services.AddScoped(typeof(OracleConnection), () =>
{
// initialize the connection here
return connection;
});
services.AddScoped<MyDal>();
Then, in your data portal operation method (such as create, fetch, etc.), inject your DAL:
[Fetch]
private void Fetch([Inject] MyDal dal)
{
var data = dal.GetData();
}
Asp.net core rc1, dbcontext connection is closed in IStringLocalizer GetString method. Happening only when VS2015 is on Debug mode and only once at start up.
I am using Autofac DI, but same issue (without autofac) using the buildin DI.
When I am running the App in debug mode and only at start up, producing the following error. When I refresh the browser all fine, no errors. If I run the App without debugging, no errors, everything runs normally.
Something's wrong with the debugging threat and the DI? Any ideas?
Error on browser:
A database operation failed while processing the request.
InvalidOperationException: ExecuteReader requires an open and
available Connection. The connection's current state is closed.
Output window:
Microsoft.Data.Entity.Storage.Internal.RelationalCommandBuilderFactory: Information: Executed DbCommand (55ms) [Parameters=[#___cultureName_0='?', #__name_1='?'], CommandType='Text', CommandTimeout='30']
SELECT TOP(1) [l].[CultureId], [l].[Name], [l].[Value]
FROM [UIResources] AS [l]
WHERE ([l].[CultureId] = #___cultureName_0) AND ([l].[Name] = #__name_1)
Microsoft.Data.Entity.Query.Internal.QueryCompiler: Error: An exception occurred in the database while iterating the results of a query.
System.NullReferenceException: Not Specified object reference to an instance object.
σε System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
σε System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
σε System.Data.SqlClient.SqlConnection.Open()
σε Microsoft.Data.Entity.Storage.RelationalConnection.Open()
σε Microsoft.Data.Entity.Query.Internal.QueryingEnumerable.Enumerator.MoveNext()
σε System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
σε System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
σε lambda_method(Closure , QueryContext )
σε Microsoft.Data.Entity.Query.Internal.QueryCompiler.<>c__DisplayClass18_1`1.<CompileQuery>b__1(QueryContext qc)
Microsoft.Data.Entity.Query.Internal.QueryCompiler: Error: An exception occurred in the database while iterating the results of a query.
System.InvalidOperationException: ExecuteReader requires an open and available Connection. The connection's current state is closed.
at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
at Microsoft.Data.Entity.Storage.Internal.RelationalCommand.<>c__DisplayClass17_0.<ExecuteReader>b__0(DbCommand cmd, IRelationalConnection con)
at Microsoft.Data.Entity.Storage.Internal.RelationalCommand.Execute[T](IRelationalConnection connection, Func`3 action, String executeMethod, Boolean openConnection, Boolean closeConnection)
at Microsoft.Data.Entity.Storage.Internal.RelationalCommand.ExecuteReader(IRelationalConnection connection, Boolean manageConnection)
at Microsoft.Data.Entity.Query.Internal.QueryingEnumerable.Enumerator.MoveNext()
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
at lambda_method(Closure , QueryContext )
at Microsoft.Data.Entity.Query.Internal.QueryCompiler.<>c__DisplayClass18_1`1.<CompileQuery>b__1(QueryContext qc)
Exception thrown: 'System.NullReferenceException' in EntityFramework.Core.dll
Exception thrown: 'System.InvalidOperationException' in EntityFramework.Core.dll
This is my startup:
public IServiceProvider ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddMvc().AddViewLocalization();
services.AddMvc().AddDataAnnotationsLocalization();
services.AddLocalization();
// Create the Autofac container builder.
var builder = new ContainerBuilder();
// Populate the services from the collection.
// This have to come First.
builder.Populate(services);
// Register dependencies.
builder.RegisterType<AuthMessageSender>().As<IEmailSender>().InstancePerLifetimeScope();
builder.RegisterType<AuthMessageSender>().As<ISmsSender>().InstancePerLifetimeScope();
builder.RegisterType<DataInitializer>().As<IDataInitializer>().InstancePerLifetimeScope();
builder.RegisterType<CultureHelper>().As<ICultureHelper>().InstancePerLifetimeScope();
builder.RegisterType<RouteRequestCultureProvider>().InstancePerLifetimeScope();
builder.RegisterType<CultureActionFilter>().InstancePerLifetimeScope();
builder.RegisterType<DbStringLocalizerFactory>().As<IStringLocalizerFactory>().InstancePerLifetimeScope();
// DbStringLocalizer registers with InstancePerDependency,
// because localization requires a new instance of IStringLocalizer created in the IStringLocalizerFactory.
builder.RegisterType<DbStringLocalizer>().As<IStringLocalizer>().InstancePerDependency();
// Build the container.
var container = builder.Build();
// Return the IServiceProvider resolved from the container.
return container.Resolve<IServiceProvider>();
}
This is Localization implementation:
public class DbStringLocalizerFactory : IStringLocalizerFactory
{
private IServiceProvider _serviceProvider;
public DbStringLocalizerFactory(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
public IStringLocalizer Create(Type resourceSource)
{
return _serviceProvider.GetService<IStringLocalizer>();
}
public IStringLocalizer Create(string baseName, string location)
{
return _serviceProvider.GetService<IStringLocalizer>();
}
}
public class DbStringLocalizer : IStringLocalizer
{
private ApplicationDbContext _db;
private string _cultureName;
public DbStringLocalizer(ApplicationDbContext db)
: this(db, CultureInfo.CurrentCulture)
{
}
public DbStringLocalizer(ApplicationDbContext db, CultureInfo cultureInfo)
{
_db = db;
_cultureName = cultureInfo.Name;
}
public LocalizedString this[string name]
{
get
{
var value = GetString(name);
return new LocalizedString(name, value ?? name, resourceNotFound: value == null);
}
}
public LocalizedString this[string name, params object[] arguments]
{
get
{
var format = GetString(name);
var value = string.Format(format ?? name, arguments);
return new LocalizedString(name, value, resourceNotFound: format == null);
}
}
private string GetString(string name)
{
//try
//{
var query = _db.UIResources.Where(l => l.CultureId == _cultureName);
var value = query.FirstOrDefault(l => l.Name == name);
return value?.Value;
//}
//catch
//{
// return null;
//}
}
public IEnumerable<LocalizedString> GetAllStrings(bool includeAncestorCultures)
{
return _db.UIResources.Where(l => l.CultureId == _cultureName)
.Select(l => new LocalizedString(l.Name, l.Value, true));
}
public IStringLocalizer WithCulture(CultureInfo culture)
{
return new DbStringLocalizer(_db, culture);
}
}
The IDataInitializer registration as scoped service in
builder.RegisterType<DataInitializer>().As<IDataInitializer>().InstancePerLifetimeScope();
seems odd, as a data initializer is expected to be called only once, when the application starts to ensure the database is created and filled with data required to run the application or after an update.
I suspect you dispose the scoped db context somewhere inside your DataInitializer class, so it becomes unavailable during your request because your IoC container will always return the same instance for the duration of the request.
I am working rest services with spring and hibernate,for updating employee data using below code, but when run I got below error
{
"code": 0,
"message": "org.hibernate.HibernateException: illegally attempted to associate a proxy with two open Sessions"
}
DataDaoImpl.java
#Override
public Employee getEntityById(long id) throws Exception {
session = sessionFactory.openSession();
Employee employee = (Employee) session.load(Employee.class,
new Long(id));
tx = session.getTransaction();
session.beginTransaction();
tx.commit();
return employee;
}
RestController.jav
#RequestMapping(value = "/save/{id}", method = RequestMethod.POST,consumes = MediaType.APPLICATION_JSON_VALUE)
public #ResponseBody
Status saveUser(#PathVariable("id") long id,#RequestBody Employee employee) {
Employee employeeupdate = null;
try {
employeeupdate = dataServices.getEntityById(id);
employeeupdate.setFirstName(employee.getFirstName());
employeeupdate.setLastName(employee.getLastName());
employeeupdate.setEmail(employee.getEmail());
employeeupdate.setPhone(employee.getPhone());
dataServices.updateEntity(employeeupdate);
return new Status(1, "Employee updated Successfully !");
} catch (Exception e) {
// e.printStackTrace();
return new Status(0, e.toString());
}
}
#Override
public boolean updateEntity(Employee employeeupdate) throws Exception {
session = sessionFactory.openSession();
tx = session.beginTransaction();
session.update(employeeupdate);
tx.commit();
session.close();
return false;
}
What mistake have I done here?
In the getEntityById(...) method the session is not closed. Close the session using session.close(); before returning the employee and try.
Don't open 2 sessions. Open just one and use it.
place the
session = sessionFactory.openSession();
tx = session.beginTransaction();
In the beginning of saveUser() and pass the session/transaction to the methods. Actually you don't need transaction in the getEntityById() - you change nothing.
Appart of #StanislavL answer, there are some errors in your code getEntityById() should be (you need close a session and place a transaction above load()).
#Override
public Employee getEntityById(long id) throws Exception {
session = sessionFactory.openSession();
tx = session.getTransaction();
session.beginTransaction();
Employee employee = (Employee) session.load(Employee.class,
new Long(id));
tx.commit();
session.close();
return employee;
}
But this variant is not very correct too, you should catch an exception use a finally block and rollback a transaction. The best way is using this pattern doInTransaction().
Update
It is better to get an entity this way
Employee employee = (Employee) session.get(Employee.class, id);
In short, you should not create session in your DAO.
Given that you are using Spring, you should avoid manually creating the Session/Transaction. Please make use of Spring to manage the transaction and session creation, by relying on Spring's transaction control and things like LocalEntityManagerFactoryBean
I am trying to deploy an ASP.NET MVC 4 application with Entity Framework 4.4 to a shared web hosting (GoDaddy-4GH platform). In GoDaddy I can't create databases using the application code I have to create it via their control panel, which I did.
I want to use the migration feature to allow my database to evolve without manually modify the schema.
I've use a combination of IDatabaseInitializer and DbMigrationsConfiguration. The db initializer simply migrates to the latest version.
The problem is that during the update process EF checks whether the database exists using the EnsureDatabaseExists method, and if for some reason it decides that is does not, then it goes ahead and tries to create a new database which of course fails.
How can I debug why the EnsureDatabaseExists returns false?
Is it possible to override this behavior? (from looking at the code with reflection it does not seem that way)
DBMigration implementation
public class DBMigrationInitializaer : IDatabaseInitializer<AppDbContext> {
public void InitializeDatabase(AppDbContext context) {
bool dbExists;
var mig = new DbMigrator(new MigrationConfiguration());
mig.Update();
Seed(context);
context.SaveChanges();
}
protected virtual void Seed(AppDbContext context) {
// TODO: put here your seed creation
}
Exception stack trace
[SqlException (0x80131904): CREATE DATABASE permission denied in database 'master'.]
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +2072894
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +5061932
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() +234
System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +2275
System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async) +228
System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) +326
System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +137
System.Data.SqlClient.<>c__DisplayClassa.<DbCreateDatabase>b__7(SqlConnection conn) +38
System.Data.SqlClient.SqlProviderServices.UsingConnection(SqlConnection sqlConnection, Action`1 act) +98
System.Data.SqlClient.SqlProviderServices.UsingMasterConnection(SqlConnection sqlConnection, Action`1 act) +349
System.Data.SqlClient.SqlProviderServices.DbCreateDatabase(DbConnection connection, Nullable`1 commandTimeout, StoreItemCollection storeItemCollection) +315
System.Data.Objects.ObjectContext.CreateDatabase() +84
System.Data.Entity.Migrations.Utilities.DatabaseCreator.Create(DbConnection connection) +73
System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists() +76
System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration) +44
System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update() +12
MvcApplication1.Models.MyDBInitializaer.InitializeDatabase(AppContext context) in MyDBInitializaer.cs:31
System.Data.Entity.<>c__DisplayClass2`1.<SetInitializerInternal>b__0(DbContext c) +75
System.Data.Entity.Internal.<>c__DisplayClass8.<PerformDatabaseInitialization>b__6() +19
System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action) +72
System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization() +186
System.Data.Entity.Internal.LazyInternalContext.<InitializeDatabase>b__4(InternalContext c) +7
System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input) +118
System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action) +190
System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase() +73
System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +28
System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +56
System.Data.Entity.Internal.Linq.InternalSet`1.GetEnumerator() +15
System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator() +40
System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +315
System.Linq.Enumerable.ToList(IEnumerable`1 source) +58
MvcApplication1.Controllers.EmployeeController.Index() in EmployeeController.cs:21
Thank you,
Ido
I have the same problem with my webhost (amen.fr). During SEVERAL days I looked and I realized that there's something in the implementation of the class DBMigrator and sql server configuring at the host which causes this dysfunction. "System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists" unable to check the correct information. That why all transactions with the database are made with my data context. Here is the code that allows me to make semi-automatic migration :
public class Migrator
{
public static void RunMigrations()
{
//Configuration configuration = new Configuration();
//configuration.ContextType = typeof(BOContext);//TODO Change to your DbContext
//configuration.MigrationsAssembly = configuration.ContextType.Assembly;
//configuration.MigrationsNamespace = "BO.Domain.Migrations";//TODO Namespace that contains your migrations classes
//configuration.TargetDatabase = new DbConnectionInfo(context.Database.Connection.ConnectionString, "System.Data.SqlClient");
//DbSeederMigrator<BOContext> migrator = new DbSeederMigrator<BOContext>(configuration);
//migrator.MigrateToLatestVersion();
using (BOContext context = new BOContext())
{
Configuration configuration = new Configuration();
configuration.ContextType = typeof(BOContext);//TODO Change to your DbContext
configuration.MigrationsAssembly = configuration.ContextType.Assembly;
configuration.MigrationsNamespace = "CodeFirstMembershipSharp.Migrations";//TODO Namespace that contains your migrations classes
configuration.TargetDatabase = new DbConnectionInfo(context.Database.Connection.ConnectionString, "System.Data.SqlClient");
DbMigrator migrator = new DbMigrator(configuration);
MigratorScriptingDecorator scriptor = new MigratorScriptingDecorator(migrator);
var lm = migrator.GetLocalMigrations();// get local migration
var dm = context.Database.SqlQuery<Migration>("select MigrationId from dbo.__MigrationHistory").Select(o => o.MigrationId); // get database migration
List<string> pm = lm.Except(dm).ToList();// buil and set pending migration
if (pm.Any())// Peding migration exists
{
string source = dm.Any() ? dm.OrderBy(o => o).Last() : DbMigrator.InitialDatabase;// get last to set source migration
string target = pm.OrderBy(o => o).Last(); /// gest last to set target migration
string sql = scriptor.ScriptUpdate(source, target); // buit sql script migration
try { context.Database.ExecuteSqlCommand(sql); } // execute sql script migration
catch (Exception e)
{
string spm = "Pending migrations : " + String.Join(";", pm.ToArray());
string sdm = "Database migrations : " + String.Join(";", pm.ToArray());
string[] tmp = { e.Message, spm, sdm, "Source : " + source, "Target : " + target, sql };
throw new Exception(String.Join("\n-----------------\n", tmp));
}
}
}
//// TODO : code seed here
//Migrator.Seed();
}
protected static void Seed()
{
//using (BOContext context = new BOContext())
//{
// if (!context.Users.Any())
// {
// MembershipCreateStatus Status;
// Membership.CreateUser("Demo", "123456", "demo#demo.com", null, null, true, out Status);
// if (!context.Roles.Any(o => o.RoleName == "Admin"))
// {
// Roles.CreateRole("Admin");
// Roles.AddUserToRole("Demo", "Admin");
// }
// }
//}
}laguna-veneta
}
Is it possible to get the identity of a record after the SaveChanges method but prior to the TransactionScope.Complete call using the DbContext? Do we need to cast to ObjectContext to get this functionality?
We need this when we send a message to a transactional queue on a separate server using MSDTC.
Example:
static void Main(string[] args)
{
const string qName = ".\\Private$\\DbContextTransactions";
var db = new BlogDb();
// force db creation & create queue
Console.WriteLine("Count={0}", db.Posts.Count());
if (!MessageQueue.Exists(qName))
MessageQueue.Create(qName, true); // transactional
try
{
using (var t = new TransactionScope())
using (MessageQueue q = new MessageQueue(qName))
{
var post = new Post() { Title = "Test " + DateTime.Now.Ticks.ToString() };
db.Posts.Add(post);
db.SaveChanges();
// TODO: how do we get post.Id (updated identity)?
q.Send(post.Id, MessageQueueTransactionType.Automatic);
t.Complete();
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
Console.WriteLine("Count={0}", db.Posts.Count());
Console.ReadLine();
}
The identity value should always be set after you call SaveChanges, even inside a TransactionScope.