I have an object I am trying to update using Entity Framework 5.
Once I retrieve the existing object and go to update the fields, it correctly updates the base object "coach", but fails to update the Address object and instead inserts it again instead of updating it with a new primary key even though it has been passed the existing primary key to use again.
Any help is appreciated.
Below is a dumbed down version of the code:
using (AltairEntities context = new AltairEntities())
{
dtlCoach coach = context.dtlCoaches.FirstOrDefault(x => x.CoachID == coachId);
coach.Name = "Bob";
coach.Description = "sample";
coach.dtlCoachAddresses.Add(PrepareAddress(coach.dtlCoachAddresses.First().CoachAddressID));
context.Database.Connection.Open();
context.Entry(coach).State = EntityState.Modified;
context.SaveChanges();
}
public static dtlCoachAddress PrepareAddress(int existingId)
{
dtlCoachAddress newAddress = new dtlCoachAddress();
try
{
newAddress.CoachAddressID = existingId;
newAddress.AddressLine1 = "Line 1";
newAddress.AddressLine2 = "Line 2";
return newAddress;
}
catch (Exception ex)
{
throw ex;
}
}
UPDATE:
So I have found if I feed the existing dtlCoachAddress entity from inside the dtlCoach entity into the PrepareAddress function as a parameter instead of declaring the object as new, it updates correctly.
What is the difference between the dtlCoachAddress object from the entity and the dtlCoachAddress object defined from new, if I pass it all the same parameters? But the two define if the object gets inserted or updated?
I am not sure how you have arranged PKs and FKs in your entities. So this solution has a few assumptions.
Updating again to match OPs methods.
using (AltairEntities context = new AltairEntities())
{
dtlCoach coach = context.dtlCoaches.FirstOrDefault(x => x.CoachID == coachId);
coach.Name = "Bob";
coach.Description = "sample";
//coach.dtlCoachAddresses.Add(PrepareAddress(coach.dtlCoachAddresses.First().CoachAddressID));
//context.Database.Connection.Open();
//context.Entry(coach).State = EntityState.Modified;
var address = context.dtlCoachAddresses.FirstOrDefault(a => a.CoachAddressID == coachId);
if(address != null)
{
address.AddressLine1 = "Line 1";
address.AddressLine2 = "Line 2";
}
context.SaveChanges();
}
/*This function is not required
public static dtlCoachAddress PrepareAddress(int existingId)
{
using (AltairEntities context = new AltairEntities())
{
var address = context.dtlCoachAddresses.FirstOrDefault(a => a.CoachAddressID == coachId);
if(address != null)
{
address.AddressLine1 = "Line 1";
address.AddressLine2 = "Line 2";
context.SaveChanges();//update an existing address.
}
}
catch (Exception ex)
{
throw ex;
}
}*/
Related
I'm working on ASP.NET Boilerplate. I have the problem where I try to get a record from a table called Buildings and make an update on it. I get the record from database by:
var buildingApp = _buildingsAppService.getBuildingsById(buildingInput);
And after that, I make some changes on the data as follows:
buildingApp.streetName = Request["buildingaddress"];
buildingApp.isInHoush = Convert.ToBoolean(Request["buildingOutput.isInHoush"]);
buildingApp.houshName = Request["HoushName"];
And then copy the buildingApp to another object, which has the same properties, in order to pass the new object to update method as follows:
var updateBuildingInput = new UpdateBuidlingsInput()
{
Id = buildingApp.Id,
buildingID = buildingApp.buildingID,
numOfBuildingUnits = buildingApp.numOfBuildingUnits,
numOfFloors = buildingApp.numOfFloors,
streetName = buildingApp.streetName,
buildingNo = buildingApp.buildingNo,
neighborhoodID = buildingApp.neighborhoodID,
buildingTypeID = buildingApp.buildingTypeID,
GISMAP = buildingApp.GISMAP,
houshProperty = buildingApp.houshProperty,
houshName = buildingApp.houshName,
X = buildingApp.X,
Y = buildingApp.Y,
buildingName = buildingApp.buildingName,
isInHoush = buildingApp.isInHoush,
buildingUsesID = buildingApp.buildingUsesID
};
And the update method is as follows:
_buildingsAppService.update(updateBuildingInput);
The problem is when it executes the previous line, I get the following error:
System.InvalidOperationException: 'Attaching an entity of type 'TaawonMVC.Models.Buildings' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate.'
I can see that when I initialize the object updateBuildingInput manually, the update method runs without error. But when it depends on the object obtained from database using buildingApp, the error happens. It seems like the get method gets data from database and keeps holding on to the record from database, and when I try to update the same record, the conflict happens. This is the whole action where all of get and update happens:
public ActionResult UpdateApplication (UpdateApplicationsInput model)
{
var updateApplication = new UpdateApplicationsInput();
updateApplication.buildingId = Convert.ToInt32(Request["buildingnumber"]);
updateApplication.buildingUnitId = Convert.ToInt32(Request["dropDownBuildingUnitApp"]);
//==== get building and unit related to application for update ======
var buildingInput = new GetBuidlingsInput()
{
Id = updateApplication.buildingId
};
var buildingUnitInput = new GetBuildingUnitsInput()
{
Id = updateApplication.buildingUnitId
};
var buildingApp = _buildingsAppService.getBuildingsById(buildingInput);
var buildingUnitApp = _buildingUnitsAppService.GetBuildingUnitsById(buildingUnitInput);
buildingApp.streetName = Request["buildingaddress"];
buildingApp.isInHoush = Convert.ToBoolean(Request["buildingOutput.isInHoush"]);
buildingApp.houshName = Request["HoushName"];
// buildingUnitApp.BuildingId = updateApplication.buildingId;
buildingUnitApp.ResidenceStatus = Request["residentstatus"];
// copy object getBuildingUnitInput to updateBuildingUnitInput
var updateBuildingUnitInput = new UpdateBuildingUnitsInput()
{
BuildingId = buildingUnitApp.BuildingId,
ResidentName = buildingUnitApp.ResidentName,
ResidenceStatus = buildingUnitApp.ResidenceStatus,
NumberOfFamilyMembers = buildingUnitApp.NumberOfFamilyMembers,
Floor = buildingUnitApp.Floor,
UnitContentsIds = buildingUnitApp.UnitContentsIds
};
//============================================
// copy object from getBuildingOutput to updateBuildingInput
var updateBuildingInput = new UpdateBuidlingsInput()
{
Id = buildingApp.Id,
buildingID = buildingApp.buildingID,
numOfBuildingUnits = buildingApp.numOfBuildingUnits,
numOfFloors = buildingApp.numOfFloors,
streetName = buildingApp.streetName,
buildingNo = buildingApp.buildingNo,
neighborhoodID = buildingApp.neighborhoodID,
buildingTypeID = buildingApp.buildingTypeID,
GISMAP = buildingApp.GISMAP,
houshProperty = buildingApp.houshProperty,
houshName = buildingApp.houshName,
X = buildingApp.X,
Y = buildingApp.Y,
buildingName = buildingApp.buildingName,
isInHoush = buildingApp.isInHoush,
buildingUsesID = buildingApp.buildingUsesID
};
//======================================================
updateApplication.Id = Convert.ToInt32(Request["applicationId"]);
updateApplication.fullName = model.fullName;
updateApplication.phoneNumber1 = model.phoneNumber1;
updateApplication.phoneNumber2 = model.phoneNumber2;
updateApplication.isThereFundingOrPreviousRestoration = model.isThereFundingOrPreviousRestoration;
updateApplication.isThereInterestedRepairingEntity = model.isThereInterestedRepairingEntity;
updateApplication.housingSince = model.housingSince;
updateApplication.previousRestorationSource = model.previousRestorationSource;
updateApplication.interestedRepairingEntityName = model.interestedRepairingEntityName;
updateApplication.PropertyOwnerShipId = Convert.ToInt32(Request["PropertyOwnerShip"]);
updateApplication.otherOwnershipType = model.otherOwnershipType;
updateApplication.interventionTypeId = Convert.ToInt32(Request["interventionTypeName"]);
updateApplication.otherRestorationType = model.otherRestorationType;
updateApplication.propertyStatusDescription = model.propertyStatusDescription;
updateApplication.requiredRestoration = model.requiredRestoration;
updateApplication.buildingId = Convert.ToInt32(Request["buildingnumber"]);
updateApplication.buildingUnitId = Convert.ToInt32(Request["dropDownBuildingUnitApp"]);
// ==== get of restoration types which it is multi select drop down list ======
var restorationTypes = Request["example-getting-started"];
string[] restorationTypesSplited = restorationTypes.Split(',');
byte[] restorationTypesArray = new byte[restorationTypesSplited.Length];
for (var i = 0; i < restorationTypesArray.Length; i++)
{
restorationTypesArray[i] = Convert.ToByte(restorationTypesSplited[i]);
}
updateApplication.restorationTypeIds = restorationTypesArray;
// ====== end of RestorationTypes
_buildingsAppService.update(updateBuildingInput);
_applicationsAppService.Update(updateApplication);
// _buildingUnitsAppService.Update(updateBuildingUnitInput);
// ==== get list of applications ==============
var applicationsUpdate = _applicationsAppService.getAllApplications();
var applicationsViewModel = new ApplicationsViewModel()
{
Applications = applicationsUpdate
};
return View("Applications", applicationsViewModel);
}
How ASP.NET Boilerplate template, which I use, makes CRUD Operation to database:
public class BuildingsManager : DomainService, IBuildingsManager
{
private readonly IRepository<Buildings> _repositoryBuildings;
public BuildingsManager(IRepository<Buildings> repositoryBuildings)
{
_repositoryBuildings = repositoryBuildings;
}
// create new building in table buildings .
public async Task<Buildings> create(Buildings entity)
{
var building = _repositoryBuildings.FirstOrDefault(x => x.Id == entity.Id);
if(building!=null)
{
throw new UserFriendlyException("Building is already exist");
}
else
{
return await _repositoryBuildings.InsertAsync(entity);
}
}
// delete a building from buildings table .
public void delete(int id)
{
try
{
var building = _repositoryBuildings.Get(id);
_repositoryBuildings.Delete(building);
}
catch (Exception)
{
throw new UserFriendlyException("Building is not exist");
}
}
public IEnumerable<Buildings> getAllList()
{
return _repositoryBuildings.GetAllIncluding(b => b.BuildingType, n => n.NeighboorHood,u=>u.BuildingUses);
}
public Buildings getBuildingsById(int id)
{
return _repositoryBuildings.Get(id);
}
public void update(Buildings entity)
{
_repositoryBuildings.Update(entity);
}
}
How can I solve this problem? Many thanks for help.
By creating a new entity (updateBuildingInput) with the same primary key as one you have already read in your context, Entity will throw an error when you attempt an operation on the new entity (as you have seen) as it is already tracking an entity with that primary key in the context.
If _buildingsAppService is a DbContext and all you need to do is make some changes to an entity, you can:
Read the entity
Make changes directly to that entity object
Call _buildingsAppService.SaveChanges()
SaveChanges() will:
Saves all changes made in this context to the underlying database.
When getting the record from db you can use .AsNoTracking()
Or if you really need to update an attached entity first locate the attached copy and detach it, then modify and update;
public async Task<bool> UpdateAsync<T>(T entity)
where T : class, IHasId
{
// check if entity is being tracked
var local = _context.Set<T>().Local.FirstOrDefault(x => x.Id.Equals(entity.Id));
// if entity is tracked detach it from context
if (local != null)
_context.Entry<T>(local).State = EntityState.Detached;
_context.Attach(entity).State = EntityState.Modified;
var result = await _context.SaveChangesAsync();
// detach entity if it was not tracked, otherwise it will be kept tracking
if(local == null)
_context.Entry(entity).State = EntityState.Detached;
return result > 0;
}
btw, IHasId is a simple interface to make Id property accessible for generic types;
public interface IHasId {
int Id { get; set; }
}
Use .AsNoTracking():
public class BuildingsManager : DomainService, IBuildingsManager
{
public Buildings getBuildingsById(int id)
{
return _repositoryBuildings.GetAll().AsNoTracking().First(b => b.Id == id);
}
// ...
}
I pull student data from 2 databases. 1 from an online SOAP API which can handle async calls and 1 from a local DB with an older services that doesnt support async.
I compare these databases and write the differences in a local sqlDB through EF.
Problem:
I get double entries in my EF DB. He puts the correct data and amount in arrays inside the method, but it looks like once he hits the db.savechanges() he jumps back up a few line and saves again.
I don't even know where this extra thread comes from.
Some code might be still there from numerous tries to solve it. For instance I tried with addrange but I get an error when he tries to add the FullVarianceList.
public async Task<bool> FullStudentCompare(string date) //format DD/MM/YYYY
{
try
{
//DB context
using (var db = new SchoolDbContext())
{
//GET DATA
//SMT (async)
List<SmtStudent> smtStdudentList = await GetAllSmartschoolStudents();
//Wisa (sync)
//on date, or if emty on current systemdate
List<WisaStudent> wisaList;
if (date == "")
{
wisaList = GetWisaStudentData(DateTime.Now.ToShortDateString());
}
else
{
wisaList = GetWisaStudentData(date);
}
//Flags and props needed for DB entry after compare
bool existsInLocalDb = false;
List<Variance> vList = new List<Variance>();
//Full list to add to DB outside foreach
List<Variance> fullVarianceList = new List<Variance>();
//Full List of new Students to write to DB outside foreach
List<DbStudent> fullStudentList = new List<DbStudent>();
//Compare lists
foreach (WisaStudent wstd in wisaList)
{
//determine correct classCode
string klasCode;
if (wstd.klasgroep.Trim() == "Klasgroep 00")
{
klasCode = wstd.klas.Trim();
}
else
{
klasCode = wstd.klasgroep.Trim();
}
//Create SmtStudent object for compare
SmtStudent tempStd = new SmtStudent(true,
wstd.voornaam.Trim(),
wstd.naam.Trim(),
wstd.stamboeknummer.Trim(),
wstd.geslacht.Trim(),
wstd.geboortedatum.Trim(),
wstd.straat.Trim(),
wstd.huisnummer.Trim(),
wstd.busnummer.Trim(),
wstd.postcode.Trim(),
wstd.gemeente.Trim(),
wstd.emailadres.Trim(),
wstd.GSM_nummer.Trim(),
wstd.levensbeschouwing.Trim(),
wstd.coaccountmoedervoornaam.Trim(),
wstd.coaccountmoedernaam.Trim(),
wstd.coaccountmoederemailadres.Trim(),
wstd.coaccountmoederGSM_nummer.Trim(),
wstd.coaccountvadervoornaam.Trim(),
wstd.coaccountvadernaam.Trim(),
wstd.coaccountvaderemailadres.Trim(),
wstd.coaccountvaderGSM_nummer.Trim(),
klasCode,
wstd.nationaliteit,
wstd.geboorteGemeente,
wstd.geboorteLand
);
//Find matching SmtStudent
SmtStudent smtStd = smtStdudentList.Find(i => i.Internnummer == wstd.stamboeknummer);
//Find matching Std in local DB
DbStudent dbStd = await db.Students.Where(i => i.Stamboeknummer == wstd.stamboeknummer).FirstOrDefaultAsync();
//if none exists in the local DB create an entity to update and write to DB
if (dbStd == null)
{
dbStd = new DbStudent(wstd.voornaam.Trim(),
wstd.naam.Trim(),
wstd.stamboeknummer.Trim(),
wstd.geslacht.Trim(),
wstd.geboortedatum.Trim(),
wstd.straat.Trim(),
wstd.huisnummer.Trim(),
wstd.busnummer.Trim(),
wstd.postcode.Trim(),
wstd.gemeente.Trim(),
wstd.emailadres.Trim(),
wstd.GSM_nummer.Trim(),
wstd.levensbeschouwing.Trim(),
wstd.coaccountmoedervoornaam.Trim(),
wstd.coaccountmoedernaam.Trim(),
wstd.coaccountmoederemailadres.Trim(),
wstd.coaccountmoederGSM_nummer.Trim(),
wstd.coaccountvadervoornaam.Trim(),
wstd.coaccountvadernaam.Trim(),
wstd.coaccountvaderemailadres.Trim(),
wstd.coaccountvaderGSM_nummer.Trim(),
klasCode,
wstd.loopbaanDatum,
wstd.nationaliteit,
wstd.geboorteGemeente,
wstd.geboorteLand
);
db.Students.Add(dbStd);
fullStudentList.Add(dbStd);
}
else
{
existsInLocalDb = true;
}
if (smtStd == null)
{
//Std doesn't exist in Smt -> New student
dbStd.IsNewStudent = true;
dbStd.ClassMovement = true;
//remove from wisaList
wisaList.Remove(wstd);
}
else
{
//clear vlist from previous iterations
vList.Clear();
//get all properties on the obj, cycle through them and find differences
PropertyInfo[] props = smtStd.GetType().GetProperties();
vList.AddRange(props.Select(f => new Variance
{
Property = f.Name,
ValueA = f.GetValue(smtStd),
ValueB = f.GetValue(tempStd),
Checked = false
})
.Where(v => !v.ValueA.Equals(v.ValueB) && v.ValueB != null)
.ToList());
//If the users allrdy exists in LocalDb delete all previously recorded variances
if (existsInLocalDb)
{
if (db.Variances.Where(j => j.Student.StudentId.Equals(dbStd.StudentId)).FirstOrDefault() != null)
{ //if the student allready exists we will recreate the variancelist, hence deleting all current items first
List<Variance> existingList = db.Variances.Where(j => j.Student.StudentId.Equals(dbStd.StudentId)).ToList();
foreach (Variance v in existingList)
{
db.Variances.Remove(v);
}
}
}
//Add new variances if vList is not empty
if (vList.Count > 0)
{
//Check if KlasCode is a variance -> set classmovement to true
if (vList.Where(i => i.Property == "KlasCode").FirstOrDefault() != null)
{
dbStd.ClassMovement = true;
}
else
{
dbStd.ClassMovement = false;
}
//add the StudentObject to the variance to link them 1-many
foreach (Variance v in vList)
{
v.Student = dbStd;
fullVarianceList.Add(v);
db.Variances.Add(v);
}
}
}
}
//add the full lists of variances and new students to DB
//db.Variances.AddRange(fullVarianceList);
//db.Students.AddRange(fullStudentList);
db.SaveChanges();
return true;
}
}
catch(Exception ex)
{
return false;
}
}
A couple of things:
It is important to understand that EF uses a unit of work pattern where none of the changes to the entities are persisted until SaveChanges is called which explains the "once he hits the db.Savechanges() he jumps back up" phenomenon.
When you have a 1 to many relationsship and you assign a collection of entities to a navigation property on another entity and then add that parent entity to the DbContext, EF marks those child entities to be added too. In your case dbStd is added at the line "db.Students.Add(dbStd);" and at the line "v.Student = dbStd;". This is most likely what is causing your duplicates.
I am writing an asp.net console application, which reads values from .csv file and updates our database accordingly.
Inside this code, I am creating a new instance of Contact, based on its value inside the .csv file.
If the Contact table contains the same contact (contact with similar email), then update existing contact, but if the contact does not exist then create a new one. Here is my code:-
Contact contact = new Contact()
{
RecordId = fields[RecordIdIndex],
Salutation = fields[SalutationIndex],
firstName = fields[FirstNameIndex],
LastName = fields[LastNameIndex],
Organization = fields[OrganizationIndex],
Title = fields[TitleIndex],
Phone = fields[PhoneIndex],
Email = fields[EmailIndex],
// Properties go here...
};
if (!String.IsNullOrEmpty(contact.Email) && entities.Contacts.Any(a => a.Email.ToLower() == contact.Email.ToLower()))
{
// Contact already exists. Remove old.
var dbcontact = entities.Contacts.FirstOrDefault(a => a.Email.ToLower() == contact.Email.ToLower());
int contactid = dbcontact.ID;
dbcontact = contact;
dbcontact.ID = contactid;
entities.Entry(dbcontact).State = EntityState.Modified;
}
else
{
entities.Contacts.Add(contact);
}
entities.SaveChanges();
The Contact.ID is a database id, which does not exist inside the .csv file.
The above code will raise the following exception:-
An object with the same key already exists in the
ObjectStateManager. The ObjectStateManager cannot track multiple
objects with the same key.
But I am not sure why this is happening. I only have 2 Contact objects, one with ID=0 which represents the contact info inside the .csv file, while the other is the dbcontact. Is this correct?
Doing dbcontact = contact doesn't change the tracked reference in DbContext.
Here's a clean way to handle both cases without code duplication:
private void Update(Contact contact, string[] fields)
{
contact.RecordId = fields[RecordIdIndex];
contact.Salutation = fields[SalutationIndex];
contact.firstName = fields[FirstNameIndex];
contact.LastName = fields[LastNameIndex];
contact.Organization = fields[OrganizationIndex];
contact.Title = fields[TitleIndex];
contact.Phone = fields[PhoneIndex];
contact.Email = fields[EmailIndex];
// Properties go here...
return contact;
};
public void CreateOrUpdate()
{
if (!String.IsNullOrEmpty(contact.Email) && entities.Contacts.Any(a => a.Email.ToLower() == contact.Email.ToLower()))
{
// Contact already exists
var dbcontact = entities.Contacts.FirstOrDefault(a => a.Email.ToLower() == contact.Email.ToLower());
Update(dbcontact, fields);
entities.Entry(dbcontact).State = EntityState.Modified;
}
else
{
var contact = new Contact();
Update(contact, fields);
entities.Contacts.Add(contact);
}
entities.SaveChanges();
}
When I try to retrieve an object from a local database, it throws an exception
Sequence contains no elements
However I have added sample data into database. What can cause this error?
private void BindObjectToControls()
{
Item = new Item();
//set values entered by user into UI to corresponding properties of the object
try
{
Item.Description = cbxType.Text.ToString() + ", " + tbxDesc.Text;
Item.Category = (string)cbxCategory.SelectedItem;
Item.Brand = (string)cbxBrand.SelectedItem;
Item.Price = (int)nudPrice.Value;
}
catch(NullReferenceException ex)
{
MessageBox.Show("Error" + ex);
}
}
private void Save()
{
BindObjectToControls();
ComputerUZEntities db = new ComputerUZEntities();
if (isNew)
{
db.Items.Add(Item);
}
else
{
Item myItem = (from ctx in db.Items where ctx.ItemID == Item.ItemID select ctx).Single();
//Item is global variable.
myItem.Description = Item.Description;
myItem.Category = Item.Category;
myItem.Brand = Item.Brand;
myItem.Price = Item.Price;
}
db.Items.SqlQuery(sql);
db.SaveChanges();
MessageBox.Show("Saved!");
this.Close();
}
P.S I have tried all 4 calls Single, SingleOrDefault, First, FirstOrDefault. None of them has worked.
Well the problem is not with those 4 calls. The problem is that you are creating new object everytime when BindObjectToControls() method is called. Instead of creating a new Item() inside of BindObjectToControls(), Try to make inside constructor or somewhere else.
I have the following method:
public static Int32 SaveAgent(IAgent i)
{
dc = new mcollectorDataContext(ConnectionString.GetConStr());
//check if the record exists
t_agent matchID = dc.t_agents.Where(x => x.id == i.AgentID).FirstOrDefault();
try
{
if (matchID == null)
{
// if not exists add new row
t_agent _agent = new t_agent
{
wallet = i.Wallet,
branchid = GetBranchID(i.Branch),
lastupdated = i.LastUpdated,
};
dc.t_agents.InsertOnSubmit(_agent);
dc.SubmitChanges();
return _agent.id;
}
else
{
// else update row
matchID.wallet = i.Wallet;
matchID.branchid = GetBranchID(i.Branch);
matchID.lastupdated = i.LastUpdated;
dc.SubmitChanges();
return i.AgentID;
}
}
catch (Exception)
{
throw ;
}
}
This method save new reord but when i try to update, it failed, no record can be updated, but it does not throw also an error.
How can fix that problem ??
maybe try telling entity framework that the model has been modified?
try adding this before calling submitchanges
dc.Entry(matchID).State = EntityState.Modified;