in MVC 3 can you drop the database after calling DbContext.SaveChanges ()?
My feature class:
public class BipEntities : DbContext { public DbSet<Page> Pages { get; set; } public DbSet<ImageFile> ImageFiles { get; set; } }
What I'm trying to do is insert an ImageFile entry into db, and then using auto-incremented id as the image file name, save the image file in another place. When System.IO fails, I would like to roll back the database.
BipEntities db = new BipEntities(); db.Database.Connection.Open(); DbTransaction tranx = db.Database.Connection.BeginTransaction(); ImageFile img = new ImageFile { CreatedAt = DateTime.Now }; db.ImageFiles.Add(img); db.SaveChanges(); string filename = "img" + img.Id.ToString() + ".png"; try {
However, the above code gives me this error message:
EntityConnection can only be constructed with a closed DbConnection.
source share