I have problem with insert and update by Generic Repository, in generic insert or update it hasnot problem but in many to many relation i am getting error at insert :
An entity object cannot be referenced by multiple instances of IEntityChangeTracker.
update:
The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects.
my codes are
Interface
public interface IGenericRepository<TEntity>:IDisposable
{
void Insert(TEntity entity);
void Update(TEntity entity);
}
Generic class
public class GenericRepository<TEntity> : IGenericRepository<TEntity> where TEntity : class
{
private ApplicationDbContext context=null;
private DbSet<TEntity> dbSet=null;
public GenericRepository()
{
this.context = new ApplicationDbContext();
this.dbSet = context.Set<TEntity>();
}
public GenericRepository(ApplicationDbContext context)
{
this.context = context;
this.dbSet = context.Set<TEntity>();
}
public virtual void Insert(TEntity entity)
{
Error is here---> this.context.Set<TEntity>().Add(entity);
// dbSet.Add(entity);
context.SaveChanges();
}
public virtual void Update(TEntity entity)
{
Error is here---> dbSet.Attach(entity);
context.Entry(entity).State = EntityState.Modified;
context.SaveChanges();
}
}
and control codes are
private IGenericRepository<Blog> _Repository = null;
private IGenericRepository<BlogTag> _RepositoryTag = null;
private IGenericRepository<BlogCategory> _RepositoryCategory = null;
public BlogsController()
{
this._Repository = new GenericRepository<Blog>(new DbContext());
this._RepositoryTag = new GenericRepository<BlogTag>(new DbContext());
this._RepositoryCategory = new GenericRepository<BlogCategory>(new DbContext());
}
public async Task<ActionResult> Create([Bind(Include = "BlogID,BlogTitle,BlogContent,VisitCount,Preview")] Blog blog
,string[] SelectedTags,string[] SelectedCategories, HttpPostedFileBase files)
{
if (SelectedTags != null)
{
blog.BlogTags = new List<BlogTag>();
foreach (var tag in SelectedTags)
{
var tagToAdd = _RepositoryTag.GetById(int.Parse(tag));
blog.BlogTags.Add(tagToAdd);
}
}
if (SelectedCategories != null)
{
blog.BlogCategories = new List<BlogCategory>();
foreach (var cat in SelectedCategories)
{
var catToAdd = _RepositoryCategory.GetById(int.Parse(cat));
blog.BlogCategories.Add(catToAdd);
}
}
if (ModelState.IsValid)
{
blog.DateTimeInsert = DateTime.UtcNow;
blog.DateTimeModify = DateTime.UtcNow;
blog.ImagePath= files != null ? Path.GetFileName(files.FileName) : "";
blog.BlogContent = HttpUtility.HtmlEncode(blog.BlogContent);
_Repository.Insert(blog);
return RedirectToAction("Index");
}
ViewBag.BlogTags = new SelectList(_RepositoryTag.Get(), "BlogTagID", "TagName");
ViewBag.BlogCategories = new SelectList(_RepositoryCategory.Get(), "BlogCategoryID", "CategoriesName");
return View(blog);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Edit([Bind(Include = "BlogID,BlogTitle,BlogContent,VisitCount,Preview")] Blog blog
, string[] SelectedTags, string[] SelectedCategories, HttpPostedFileBase files)
{
if (Request["BlogID"] == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
int id = int.Parse(Request["BlogID"].ToString());
var blogsToUpdate = _Repository.Query(i => i.BlogID == id, null).Include(t => t.BlogTags).Include(t => t.BlogCategories).Single();
if (TryUpdateModel(blogsToUpdate, "",
new string[] { "BlogID", "BlogTitle", "BlogContent", "VisitCount","Preview" }))
{
try
{
UpdateInstructorCourses(SelectedTags, SelectedCategories, blogsToUpdate);
blogsToUpdate.DateTimeModify = DateTime.UtcNow;
blogsToUpdate.DateTimeInsert = DateTime.UtcNow;
blogsToUpdate.BlogContent = HttpUtility.HtmlEncode(blogsToUpdate.BlogContent);
await _Repository.UpdateAsync(blogsToUpdate, d => d.BlogTitle, d => d.VisitCount, d => d.BlogContent, d => d.ImagePath);
return RedirectToAction("Index");
}
catch (RetryLimitExceededException /* dex */)
{
//Log the error (uncomment dex variable name and add a line here to write a log.
ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
}
}
AssignedDDLCHKBoxValues(blogsToUpdate);
return View(blogsToUpdate);
}
private void UpdateInstructorCourses(string[] SelectedTags, string[] SelectedCategories, Blog blogsToUpdate)
{
if (SelectedTags == null)
{
blogsToUpdate.BlogTags = new List<BlogTag>();
return;
}
if (SelectedCategories == null)
{
blogsToUpdate.BlogCategories = new List<BlogCategory>();
return;
}
var SelectedTagsHS = new HashSet<string>(SelectedTags);
var SelectedCategoriesHS = new HashSet<string>(SelectedCategories);
var blogTags = new HashSet<int>(blogsToUpdate.BlogTags.Select(c => c.BlogTagID));
foreach (var tag in _RepositoryTag.Get())
{
if (SelectedTagsHS.Contains(tag.BlogTagID.ToString()))
{
if (!blogTags.Contains(tag.BlogTagID))
{
blogsToUpdate.BlogTags.Add(tag);
}
}//if
else
{
if (blogTags.Contains(tag.BlogTagID))
{
blogsToUpdate.BlogTags.Remove(tag);
}
}//else
}//foreach tag
var blogcategories = new HashSet<int>
(blogsToUpdate.BlogCategories.Select(c => c.BlogCategoryID));
foreach (var Category in _RepositoryCategory.Get())
{
if (SelectedCategoriesHS.Contains(Category.BlogCategoryID.ToString()))
{
if (!blogcategories.Contains(Category.BlogCategoryID))
{
blogsToUpdate.BlogCategories.Add(Category);
}
}//if
else
{
if (blogcategories.Contains(Category.BlogCategoryID))
{
blogsToUpdate.BlogCategories.Remove(Category);
}
}//else
}//foreach skill
}
Your problem is that you are using multiple contexts to work with a single Entity.
On your controller constructor you have these lines:
this._Repository = new GenericRepository<Blog>(new DbContext());
this._RepositoryTag = new GenericRepository<BlogTag>(new DbContext());
this._RepositoryCategory = new GenericRepository<BlogCategory>(new DbContext());
Here, you are creating 3 repositories 9that are supposed to work together) with 3 different contexts.
After that, you proceed to read from the RepositoryTag repository, here:
var tagToAdd = _RepositoryTag.GetById(int.Parse(tag));
When you do this, the object tagToAdd becomes attached to the context inside RepositoryTag. If you debug your list BlogTags where you add this tagToAdd you will see that you have a dynamic proxy, which means that that objetc is attached to a context.
After that, you use another context to fill repository category, here:
var catToAdd = _RepositoryCategory.GetById(int.Parse(cat));
blog.BlogCategories.Add(catToAdd);
Now, your blog object has references to 2 different contexts: the one you used to load the tags (RepositoryTag), and the one you used to load the blog category (RepositoryCategory).
Finally, you try to inser the blog usgin a third context:
_Repository.Insert(blog);
This will throw an exception because EF can't work with multiple contexts like this.
To solve this problem, simply instantiate a context before the repositories, and pass it to all yout repositories, like this:
this.context = new DbContext(); // The context you need to use for all operations you are performing here.
this._Repository = new GenericRepository<Blog>(this.context);
this._RepositoryTag = new GenericRepository<BlogTag>(this.context);
this._RepositoryCategory = new GenericRepository<BlogCategory>(this.context);
Now, don't forget you should dispose your contexts. This is why the most recommended and common approach is to use a code like this:
using (var ctx = new DbContext()) {
var repo = new GenericRepository<Blog>(ctx);
var repoTag = new GenericRepository<BlogTag>(ctx);
var repoCategory = new GenericRepository<BlogCategory>(ctx);
<the rest of your code where you build the `blog` object>
ctx.SaveChanges();
}
Related
How would I go about modifying existing code to call other controllers stored dictionary information? (without recalling the db multiple times, but just once at the start of the rest api's life).
Atm (I think) am storing the information in a dictionary (PipeMaterials) correctly. Now I'm lost on how to go about getting the information out to other controller.
Controller storing information
Controller wanting to consume information
Storing
public class MaterialsController : ControllerBase
{
public Dictionary<int, Materials> PipeMaterials;
public Dictionary<int, Rank> Ranks;
public Dictionary<int, Sumps> Sumps;
private readonly UMMClient23Context _context;
public MaterialsController(UMMClient23Context context)
{
_context = context;
LoadMaterials();
}
public void LoadMaterials()
{
PipeMaterials = new Dictionary<int, Materials>();
Task<MaterialsObjects> task = GetMaterials();
var result = task.Result;
foreach (var item in result.Ummmaterials)
{
if (!PipeMaterials.TryAdd(item.MaterialsId, item))
{
Console.Error.WriteLine("Could not load material: " + item.MaterialsName);
}
}
}
// GET: api/Materials
[HttpGet]
public async Task<MaterialsObjects> GetMaterials()
{
MaterialsObjects returnable = new MaterialsObjects();
returnable.Ummmaterials = await _context.Materials.ToListAsync();
return returnable;
}
// GET: api/MaterialDescription/5
[HttpGet("{materialsId}")]
public string GetMaterialDescription(int materialsId)
{
Materials item;
if (PipeMaterials.TryGetValue(materialsId, out item))
{
return item.MaterialsName;
}
else
{
return null;
}
//var materials = _context.Materials
// .Where(m=> m.MaterialsId == materialsId)
// .Select(m => m.MaterialsName)
// .FirstOrDefault();
}
Consuming
public class PipeController : ControllerBase
{
MaterialsController materialsController;
UMMDBHelper uMMDBHelper;
private readonly UMMClient23Context _context;
public PipeController(UMMClient23Context context)
{
_context = context;
uMMDBHelper = new UMMDBHelper(context);
}
//GET: api/Pipe
[HttpGet]
public async Task<ActionResult<IEnumerable<Data>>> Get(string value)
{
return await _context.Data.ToListAsync();
}
// GET: api/Pipe/{assestNumber}
[HttpGet("{assetNumber}")] // Make return into Object
public PipeListObject GetPipe(string assetNumber)
{
PipeListObject returnable = new PipeListObject();
Pipe Pipe = uMMDBHelper.GetPipe(assetNumber);
returnable.UmmPipes.Add(Pipe);
return returnable;
}
//GET: api/PipeMaterial/{materialId}
[HttpGet("{materialId}")]
public string GetPipeMaterial(int materialId)
{
var desc = materialsController.GetMaterialDescription(materialId);
return desc;
}
You could create a new MaterialsController directly in PipeController like
var desc = new MaterialsController(_context).GetMaterialDescription(materialId);
Or you could use HttpClient like
[HttpGet("{materialId}")]
public string GetPipeMaterial(int materialId)
{
using (var client = new HttpClient())
{
var response = await client.GetAsync("https://localhost:44317/api/MaterialDescription/5");
if (response.IsSuccessStatusCode)
{
var result = response.Content.ReadAsStringAsync().Result;
return result;
}
}
return "";
}
I currently have a multi one-to-many relationship hierarchy database tblProjects->tblLines->tblGroups->tblStations etc. And an Entity framework 6 model.
These entity framework classes all implement a base class "tblBase":
public abstract class TblBase : INotifyPropertyChanged
{
private int _id;
public int ID
{
get
{
return _id;
}
set
{
_id = value;
NotifyPropertyChanged();
}
}
private Nullable<int> _coid;
public Nullable<int> COID
{
get
{
NotifyPropertyChanged();
return _coid;
}
set
{
_coid = value;
NotifyPropertyChanged();
}
}
private string _name;
public string Name
{
get
{
return _name;
}
set
{
_name = value;
NotifyPropertyChanged();
}
}
I have a treeview that allows me to select any node as the parent type, and currently I have a method for each type that allows me to reload all the child entities.
I would like to see how this could be made generic:
private async static Task<bool> RefreshLinesAsync(LocalUser ThisUser, ProjectEntities DBContext, object Entity)
{
List<object> NonExistingNodes = new List<object>();
var bContinue = false;
var PassedEntity = Entity as TblBase;
//Scan through all DB child entities and reload their DB values
foreach (var SubEntity in DBContext.tblLines.Where(x => x.ProjectID == PassedEntity.ID).ToList())
{
await DBContext.Entry(SubEntity).ReloadAsync().ContinueWith(x =>
{
if (!x.IsFaulted)
{
if ((SubEntity.COID.GetValueOrDefault() != 0) && (SubEntity.COID.GetValueOrDefault() != ThisUser.ID))
NotifyCOIDConflict(SubEntity, new CheckedOutArgs()
{
ConflictCOID = SubEntity.COID.GetValueOrDefault()
});
bContinue = true;
}
}, TaskScheduler.FromCurrentSynchronizationContext());
if (bContinue)
//Continue to child entities method
await RefreshGroupsAsync(ThisUser, DBContext, SubEntity);
}
return true;
}
private async static Task<bool> RefreshGroupsAsync(LocalUser ThisUser, ProjectEntities DBContext, object Entity)
{
List<object> NonExistingNodes = new List<object>();
var bContinue = false;
var PassedEntity = Entity as TblBase;
foreach (var SubEntity in DBContext.tblGroups.Where(x => x.LineID == PassedEntity.ID).ToList())
{
await DBContext.Entry(SubEntity).ReloadAsync().ContinueWith(x =>
{
if (!x.IsFaulted)
{
if ((SubEntity.COID.GetValueOrDefault() != 0) && (SubEntity.COID.GetValueOrDefault() != ThisUser.ID))
NotifyCOIDConflict(SubEntity, new CheckedOutArgs()
{
ConflictCOID = SubEntity.COID.GetValueOrDefault()
});
bContinue = true;
}
}, TaskScheduler.FromCurrentSynchronizationContext());
if (bContinue)
await RefreshStationsAsync(ThisUser,DBContext, SubEntity);
}
return true;
}
The only method I can see useful is Set(), although it does not provide a Where() method, which is critical since I do not want to retrieve the entire table.
You can make your functions generic. They maybe like this one:
private async static Task<bool> RefreshLinesAsync<TEntity>(LocalUser ThisUser, ProjectEntities DBContext, TEntity Entity) where TEntity : TblBase
{
List<TEntity> NonExistingNodes = new List<TEntity>();
var bContinue = false;
var PassedEntity = Entity as TblBase;
foreach (var SubEntity in DBContext.Set<TEntity>().Where(x => (x as TblBase).ProjectID == PassedEntity.ID).ToList()) {
//Your other code here...
}
}
The where clause in function definition, make you sure that this method can be called only with subclasses of TblBase.
EDIT:
I forgot to mention that you need to cast SubEntity as TblBase inside foreach loop to use it...
EDIT (in response of comments):
If you need to get all TblBase subclasses from your entity, you cannot make your function so generic if you keep them in separate tables: It will became hardly mantainable when you have to add more subclasses.
I suggest you to use a single table through Table Per Hierarchy (see this article in MSDN) changing TblBase from abstract to concrete class, then you can get all of them this way:
var allSubClassEntities = DBContext.Set<TblBase>();
I'm currently writing integration tests using nunit for a previously untested server that was written in C# using ApiController and Entity Framework. Most of the tests run just fine, but I've ran into two that always cause the database to time out. The error messages look something like this:
System.Data.Entity.Infrastructure.DbUpdateException : An error occurred while updating the entries. See the inner exception for details.
System.Data.Entity.Core.UpdateException : An error occurred while updating the entries. See the inner exception for details.
System.Data.SqlClient.SqlException : Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
System.ComponentModel.Win32Exception : The wait operation timed out
The first test that's timing out:
[TestCase, WithinTransaction]
public async Task Patch_EditJob_Success()
{
var testJob = Data.SealingJob;
var requestData = new Job()
{
ID = testJob.ID,
Name = "UPDATED"
};
var apiResponse = await _controller.EditJob(testJob.ID, requestData);
Assert.IsInstanceOf<StatusCodeResult>(apiResponse);
Assert.AreEqual("UPDATED", testJob.Name);
}
The other test that's timing out:
[TestCase, WithinTransaction]
public async Task Post_RejectJob_Success()
{
var rejectedJob = Data.SealingJob;
var apiResponse = await _controller.RejectJob(rejectedJob.ID);
Assert.IsInstanceOf<OkResult>(apiResponse);
Assert.IsNull(rejectedJob.Organizations);
Assert.AreEqual(rejectedJob.JobStatus, JobStatus.OnHold);
_fakeEmailSender.Verify(
emailSender => emailSender.SendEmail(rejectedJob.Creator.Email, It.Is<string>(emailBody => emailBody.Contains(rejectedJob.Name)), It.IsAny<string>()),
Times.Once());
}
These are the controller methods that these tests are using:
The timeout always happens on the first call to await db.SaveChangesAsync() within the controller. Other controller methods that are being tested also call SaveChangesAsync without any problem. I've also tried calling SaveChangesAsync from within the failing tests and it works fine there. Both of these methods they are calling work normally when called from within the controller, but time out when called from the tests.
[HttpPatch]
[Route("editjob/{id}")]
public async Task<IHttpActionResult> EditJob(int id, Job job)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != job.ID)
{
return BadRequest();
}
Job existingJob = await db.Jobs
.Include(databaseJob => databaseJob.Regions)
.FirstOrDefaultAsync(databaseJob => databaseJob.ID == id);
existingJob.Name = job.Name;
// For each Region find if it already exists in the database
// If it does, use that Region, if not one will be created
for (var i = 0; i < job.Regions.Count; i++)
{
var regionId = job.Regions[i].ID;
var foundRegion = db.Regions.FirstOrDefault(databaseRegion => databaseRegion.ID == regionId);
if (foundRegion != null)
{
existingJob.Regions[i] = foundRegion;
db.Entry(existingJob.Regions[i]).State = EntityState.Unchanged;
}
}
existingJob.JobType = job.JobType;
existingJob.DesignCode = job.DesignCode;
existingJob.DesignProgram = job.DesignProgram;
existingJob.JobStatus = job.JobStatus;
existingJob.JobPriority = job.JobPriority;
existingJob.LotNumber = job.LotNumber;
existingJob.Address = job.Address;
existingJob.City = job.City;
existingJob.Subdivision = job.Subdivision;
existingJob.Model = job.Model;
existingJob.BuildingDesignerName = job.BuildingDesignerName;
existingJob.BuildingDesignerAddress = job.BuildingDesignerAddress;
existingJob.BuildingDesignerCity = job.BuildingDesignerCity;
existingJob.BuildingDesignerState = job.BuildingDesignerState;
existingJob.BuildingDesignerLicenseNumber = job.BuildingDesignerLicenseNumber;
existingJob.WindCode = job.WindCode;
existingJob.WindSpeed = job.WindSpeed;
existingJob.WindExposureCategory = job.WindExposureCategory;
existingJob.MeanRoofHeight = job.MeanRoofHeight;
existingJob.RoofLoad = job.RoofLoad;
existingJob.FloorLoad = job.FloorLoad;
existingJob.CustomerName = job.CustomerName;
try
{
await db.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!JobExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
[HttpPost]
[Route("{id}/reject")]
public async Task<IHttpActionResult> RejectJob(int id)
{
var organizations = await db.Organizations
.Include(databaseOrganization => databaseOrganization.Jobs)
.ToListAsync();
// Remove job from being shared with organizations
foreach (var organization in organizations)
{
foreach (var organizationJob in organization.Jobs)
{
if (organizationJob.ID == id)
{
organization.Jobs.Remove(organizationJob);
}
}
}
var existingJob = await db.Jobs.FindAsync(id);
existingJob.JobStatus = JobStatus.OnHold;
await db.SaveChangesAsync();
await ResetJob(id);
var jobPdfs = await DatabaseUtility.GetPdfsForJobAsync(id, db);
var notes = "";
foreach (var jobPdf in jobPdfs)
{
if (jobPdf.Notes != null)
{
notes += jobPdf.Name + ": " + jobPdf.Notes + "\n";
}
}
// Rejection email
var job = await db.Jobs
.Include(databaseJob => databaseJob.Creator)
.SingleAsync(databaseJob => databaseJob.ID == id);
_emailSender.SendEmail(
job.Creator.Email,
job.Name + " Rejected",
notes);
return Ok();
}
Other code that might be relevant:
The model being used is just a normal code-first Entity Framework class:
public class Job
{
public Job()
{
this.Regions = new List<Region>();
this.ComponentDesigns = new List<ComponentDesign>();
this.MetaPdfs = new List<Pdf>();
this.OpenedBy = new List<User>();
}
public int ID { get; set; }
public string Name { get; set; }
public List<Region> Regions { get; set; }
// etc...
}
To keep the database clean between tests, I'm using this custom attribute to wrap each one in a transaction (from http://tech.trailmax.info/2014/03/how-we-do-database-integration-tests-with-entity-framework-migrations/):
public class WithinTransactionAttribute : Attribute, ITestAction
{
private TransactionScope _transaction;
public ActionTargets Targets => ActionTargets.Test;
public void BeforeTest(ITest test)
{
_transaction = new TransactionScope();
}
public void AfterTest(ITest test)
{
_transaction.Dispose();
}
}
The database connection and controller being tested is build in setup methods before each test:
[TestFixture]
public class JobsControllerTest : IntegrationTest
{
// ...
private JobsController _controller;
private Mock<EmailSender> _fakeEmailSender;
[SetUp]
public void SetupController()
{
this._fakeEmailSender = new Mock<EmailSender>();
this._controller = new JobsController(Database, _fakeEmailSender.Object);
}
// ...
}
public class IntegrationTest
{
protected SealingServerContext Database { get; set; }
protected TestData Data { get; set; }
[SetUp]
public void SetupDatabase()
{
this.Database = new SealingServerContext();
this.Data = new TestData(Database);
}
// ...
}
This bug was apparently caused by the use of await within a TransactionScope. Following the top answer to this question, I added the TransactionScopeAsyncFlowOption.Enabled parameter when constructing the TransactionScope and the timeout issue went away.
I have a "DatabaseController" Class, which contains several repository classes, each for every table in a database, and they only perform crud operations. My issue is this.
When i delete an entry , i call the corresponding repository method. I also need to call some delete methods from other tables (repositories). What is the best approach to do this?
Example Code:
DatabaseController Class
public DatabaseController(){
this.dbContext = new WSATDbContext();
}
private IWSATGenericRepository<Restmethod> _restmethod ;
private IWSATGenericRepository<Soapservice> _soapservice ;
public IWSATGenericRepository<Restmethod> Restmethod
{
get
{
if (_restmethod == null)
{
_restmethod = new RestmethodRepository(dbContext);
}
return _restmethod;
}
}
public IWSATGenericRepository<Soapservice> Soapservice
{
get
{
if (_soapservice == null)
{
_soapservice = new SoapserviceRepository(dbContext);
}
return _soapservice;
}
}
Example Repository:
public class RestmethodRepository : IWSATGenericRepository<Restmethod>
{
public RestmethodRepository(DbContext dbContext)
{
if (dbContext == null)
throw new ArgumentNullException("Null DbContext");
this.dbContext = dbContext;
this.dbSet = dbContext.Set<Restmethod>();
}
private DbContext dbContext { get; set; }
private DbSet<Restmethod> dbSet { get; set; }
public Restmethod get(int id)
{
Restmethod restmethod = dbSet.Find(id) ;
dbContext.Entry(restmethod).Collection(s => s.methodkeywords).Load() ;
return restmethod ;
}
public Restmethod delete(int id)
{
var entity = this.get(id);
//I want to call some other repo methods here
dbContext.Entry(entity).State = EntityState.Deleted;
try
{
dbContext.SaveChanges();
}
catch (DbUpdateException e)
{
System.Diagnostics.Debug.WriteLine(e.StackTrace);
return null;
}
return entity;
}
}
}
EDIT: Is it good practice to pass the "DatabaseController" UOW class as an argument to each repository? Are there any theoretical problems with that? This is an academic project , so i'd like to stay as formal as possible.
First of all, I would like to say that I read the related posts (notably EF 4.1 SaveChanges not updating navigation or reference properties, Entity Framework Code First - Why can't I update complex properties this way?, and Entity Framework 4.1 RC (Code First) - Entity not updating over association).
However, I could not solve my problem. I am quite new to Entity Framework so I guess I must have misunderstood those posts answers.
Anyway I would be really grateful is someone could help me understand because I am quite stuck.
I have two tables :
Person
Item with a nullable PersonId and a Type
An item can have an owner, or not.
Consequently, Person has an Items property which is an IEnumerable of Item.
A person can have one only Item by type.
If the person wants to change, he can replace his current item by any other of the same type in his items :
public class MyService
{
private PersonRepo personRepo = new PersonRepo();
private ItemRepo itemRepo = new ItemRepo();
public void SwitchItems(Person person, Guid newItemId)
{
using (var uof = new UnitOfWork())
{
// Get the entities
Item newItem = itemRepo.Get(newItemId);
Item oldItem = person.Items.SingleOrDefault(i => i.Type == newItem.Type)
// Update the values
newItem.PersonId = person.Id;
oldItem.PersonId = null;
// Add or update entities
itemRepo.AddOrUpdate(oldItem);
itemRepo.AddOrUpdate(newItem);
personRepo.AddOrUpdate(person);
uof.Commit(); // only does a SaveChanges()
}
}
}
Here is the repositories structure and the AddOrUpdate method :
public class PersonRepo : RepositoryBase<Person>
{
...
}
public class RepositoryBase<TObject> where TObject : class, IEntity
{
protected MyEntities entities
{
get { return UnitOfWork.Current.Context; }
}
public virtual void AddOrUpdate(TObject entity)
{
if (entity != null)
{
var entry = entities.Entry<IEntity>(entity);
if (Exists(entity.Id))
{
if (entry.State == EntityState.Detached)
{
var set = entities.Set<TObject>();
var currentEntry = set.Find(entity.Id);
if (currentEntry != null)
{
var attachedEntry = entities.Entry(currentEntry);
attachedEntry.CurrentValues.SetValues(entity);
}
else
{
set.Attach(entity);
entry.State = EntityState.Modified;
}
}
else
entry.State = EntityState.Modified;
}
else
{
entry.State = EntityState.Added;
}
}
}
}
This works pretty well and the old and the new items' PersonId properties are correctly updated in database.
However, if I check person.Items after the SaveChanges(), the old item still appears instead of the new one and I need it to be correct in order to update the page's controls values.
Although I read the posts with the same issue I could not resolve it...
I tried lots of things, notably calling entities.Entry(person).Collection(p => p.Items).Load() but got an exception each time I tried.
If somebody has any idea please feel free, I can add some more code if needed.
Thanks a lot !
EDIT : UnitOfWork
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.Entity.Infrastructure;
using System.Data.Objects;
public class UnitOfWork : IDisposable
{
private const string _httpContextKey = "_unitOfWork";
private MyEntities _dbContext;
public static UnitOfWork Current
{
get { return (UnitOfWork)HttpContext.Current.Items[_httpContextKey]; }
}
public UnitOfWork()
{
HttpContext.Current.Items[_httpContextKey] = this;
}
public MyEntities Context
{
get
{
if (_dbContext == null)
_dbContext = new MyEntities();
return _dbContext;
}
}
public void Commit()
{
_dbContext.SaveChanges();
}
public void Dispose()
{
if (_dbContext != null)
_dbContext.Dispose();
}
}
Two solutions that worked
Solution 1 (reload from context after SaveChanges)
public partial class MyPage
{
private MyService service;
private Person person;
protected void Page_Load(object sender, EventArgs e)
{
service = new MyService();
person = service.GetCurrentPerson(Request.QueryString["id"]);
...
}
protected void SelectNewItem(object sender, EventArgs e)
{
Guid itemId = Guid.Parse(((Button)sender).Attributes["id"]);
service.SelectNewItem(person, itemId);
UpdatePage();
}
private void UpdatePage()
{
if (person != null)
person = service.GetCurrentPerson(Request.QueryString["id"]);
// Update controls values using person's properties here
}
}
public class MyService
{
private PersonRepo personRepo = new PersonRepo();
private ItemRepo itemRepo = new ItemRepo();
public void SwitchItems(Person person, Guid newItemId)
{
using (var uof = new UnitOfWork())
{
// Get the entities
Item newItem = itemRepo.Get(newItemId);
Item oldItem = person.Items.SingleOrDefault(i => i.Type == newItem.Type)
// Update the values
newItem.PersonId = person.Id;
oldItem.PersonId = null;
// Add or update entities
itemRepo.AddOrUpdate(oldItem);
itemRepo.AddOrUpdate(newItem);
personRepo.AddOrUpdate(person);
uof.Commit(); // only does a SaveChanges()
}
}
}
Solution 2 (update database AND property)
public partial class MyPage
{
private MyService service;
private Person person;
protected void Page_Load(object sender, EventArgs e)
{
service = new MyService();
person = service.GetCurrentPerson(Request.QueryString["id"]);
...
}
protected void SelectNewItem(object sender, EventArgs e)
{
Guid itemId = Guid.Parse(((Button)sender).Attributes["id"]);
service.SelectNewItem(person, itemId);
UpdatePage();
}
private void UpdatePage()
{
// Update controls values using person's properties here
}
}
public class MyService
{
private PersonRepo personRepo = new PersonRepo();
private ItemRepo itemRepo = new ItemRepo();
public void SwitchItems(Person person, Guid newItemId)
{
using (var uof = new UnitOfWork())
{
// Get the entities
Item newItem = itemRepo.Get(newItemId);
Item oldItem = person.Items.SingleOrDefault(i => i.Type == newItem.Type)
// Update the values
newItem.PersonId = person.Id;
oldItem.PersonId = null;
person.Items.Remove(oldItem);
person.Items.Add(newItem);
// Add or update entities
itemRepo.AddOrUpdate(oldItem);
itemRepo.AddOrUpdate(newItem);
personRepo.AddOrUpdate(person);
uof.Commit(); // only does a SaveChanges()
}
}
}
How about refreshing your context to make sure you have the latest db changes after the .SaveChanges() method. Pass in the entity to be refreshed an call Refresh on the context:
((IObjectContextAdapter)_dbContext).ObjectContext.Refresh(RefreshMode.StoreWins, entityPassed);
Or leave the Commit() method as is and use a more dynamic approach something like:
var changedEntities = (from item in context.ObjectStateManager.GetObjectStateEntries(
EntityState.Added
| EntityState.Deleted
| EntityState.Modified
| EntityState.Unchanged)
where item.EntityKey != null
select item.Entity);
context.Refresh(RefreshMode.StoreWins, changedEntities);
The RefreshMode.StoreWins simply indicates that the database (store) takes priority and will override client (in-memory) changes.
If the Refresh method does not work, you can consider the following:
public void RefreshEntity(T entity)
{
_dbContext.Entry<T>(entity).Reload();
}
Or if all else fails, keep it simple and Dispose of your DbContext once you're done with each transaction (In this case after SaveChanges() has been called). Then if you need to use results after a commit, treat it as a new transaction and, instantiating a fresh DbContext and load your necessary data again.
Use Transection for example.
It's working fine.
public class UnitOfWork : IUnitOfWork
{
public readonly DatabaseContext _context;
private readonly IDbTransaction _transaction;
private readonly ObjectContext _objectContext;
public UnitOfWork(DatabaseContext context)
{
_context = context as DatabaseContext ?? new DatabaseContext ();
this._objectContext = ((IObjectContextAdapter)this._context).ObjectContext;
if (this._objectContext.Connection.State != ConnectionState.Open)
{
this._objectContext.Connection.Open();
this._transaction = _objectContext.Connection.BeginTransaction();
}
}
public int Complete()
{
int result = 0;
try
{
result = _context.SaveChanges();
this._transaction.Commit();
}
catch (Exception ex)
{
Rollback();
}
return result;
}
private void Rollback()
{
this._transaction.Rollback();
foreach (var entry in this._context.ChangeTracker.Entries())
{
switch (entry.State)
{
case System.Data.Entity.EntityState.Modified:
entry.State = System.Data.Entity.EntityState.Unchanged;
break;
case System.Data.Entity.EntityState.Added:
entry.State = System.Data.Entity.EntityState.Detached;
break;
case System.Data.Entity.EntityState.Deleted:
entry.State = System.Data.Entity.EntityState.Unchanged;
break;
}
}
}
public void Dispose()
{
if (this._objectContext.Connection.State == ConnectionState.Open)
{
this._objectContext.Connection.Close();
}
_context.Dispose();
}
}