markrendle/Simple.Data — Issue #116: Connection closing / disposing too aggressive
Author: robhorvath
Created: 2011-11-10 21:25:26+00:00
State: open

Hi - I'm attempting to implement a Unit of Work pattern using NancyFx and Simple.Data, and I'm running into a problem. I'm trying to keep all of the reads/writes of the database to a single transaction for consistency (for each HttpRequest), but after I use a FindBy method, the connection on the transaction object no longer exists, and the Insert fails. I see in the source that the connection is explicitly being released, but in my scenario, I want it to hang around until I'm done with it.

What is the best approach to this problem?

Thanks,

Rob

```
internal class Program
{
private static void Main(string[] args)
{
var s = ConfigurationManager.ConnectionStrings["entities"];
var db = Simple.Data.Database.OpenConnection(s.ConnectionString);
var trans = db.BeginTransaction();
Store entity = trans.Stores.FindById(1);
var newStore = new Store() { CreatedAt = DateTime.Now, CreatedBy = 0, Name = "Rob's Other Store", FriendlyId = "1234", UpdatedAt = DateTime.Now, UpdatedBy = 0 };
newStore = trans.Stores.Insert(newStore);
trans.Commit();
}
}

public class Store
{
public int Id { get; set; }
public string FriendlyId { get; set; }
public string Name { get; set; }
public int CreatedBy { get; set; }
public DateTime CreatedAt { get; set; }
public int UpdatedBy { get; set; }
public DateTime UpdatedAt { get; set; }
}
```