Check if an insert or update was successful in Entity Framework - entity-framework

In ADO.NET, ExecuteNonQuery() "For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command" (http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery.aspx)
In EF v1, context.SaveChanges() method returns "The number of objects in an Added, Modified, or Deleted state when SaveChanges was called." (http://msdn.microsoft.com/en-us/library/bb739065.aspx)
Please tell, when multiple entities (or single entity) are added or updated to context and context.SaveChanges() method is called, how to check if actual INSERT or UPDATE was successful.
Can we assume if there was NO exception that INSERT(s) or UPDATE(s) was successful ?
Thank You

Yes, if there is no exception you may assume that the statements executed successfully.

In EntityFramework, SaveChangesAsync() returns an int.
So you can check if it is > 0 or not.
If something happens with SaveChangesAsync() it will return the number of effected rows and this means if value > 0 then true. So simply, you can have below scenerio:
INSERT
public Task<bool> CreateEntity(Entity entity){
if(entity == null)
return false;
await _dataContext.Entities.AddAsync(entity);
var created = await _dataContext.SaveChangesAsync();
return created > 0;
}
UPDATE
public async Task<bool> UpdateEntity(Entity entityToUpdate)
{
if(entityToUpdate == null)
return false;
_dataContext.Posts.Update(entityToUpdate);
var updated = await _dataContext.SaveChangesAsync();
return updated > 0;
}
DELETE
public async Task<bool> DeleteEntity(int entityId)
{
var entity = await _dataContext.Entities.FindAsync(entityId);
if (entity == null)
return false;
_dataContext.Entities.Remove(entity);
var deleted = await _dataContext.SaveChangesAsync();
return deleted > 0;
}
And in your methods, now you can simply check if that change is success or not:
For a simple MVC scenerio:
public Task<IActionResult> CreateEntity(EntityModel model)
{
if(model == null)
return StatusCode(404);
var entity = new Entity
{
attribute1 = model.attribute1,
attribute2 = model.attribute3
};
var isCreated = await _entityService.CreateEntity(entity);
if(isCreated)
{
//do something and return a view.
}
else
{
//you can return a status code, or an error view.
}
}
You can do the same practice for Update & Delete

Maybe this is not direct answer to the question, but may help.
By default all commands are encapsulated in one DbTransaction when SaveChanges method is called (Julia Lerman, Programming Entity Framework). So, or all commands will be successfully executed, or neither. That's one way to know if insert, or update or delete was successful.

define variable SaveStatus
var SaveStatus=context.SaveChanges()
then you can know if the creation has been done by getting the value of SaveStatus=1
in the case of "SaveStatus=0" it means no record has been created or affected

Related

Stack overflow exception while returning an object asynchronously

While using MongoDB C# driver with WebApi I came to the following problem. When I want to read all documents (or even just one) from the database the repo's function will get the correct data but in WebApi the object returned from the repo causes a stack overflow. I suspect that I am doing something wrong with the way the objects are returned.
WebApi where the Repo's method is called:
// GET api/<controller>
public async Task<List<Event>> Get()
{
return await _repo.FindAll();
}
// GET api/<controller>/5
public async Task<Event> Get(string id)
{
Event e = await _repo.FindById(id);
return e;
}
And corresponding methods in the Repo:
public async Task<Event> FindById(string id)
{
Event e = await _collection.Find<Event>(x => x.ID == ObjectId.Parse(id)).FirstAsync();
return e;
}
public async Task<List<Event>> FindAll()
{
var filter = new BsonDocument();
List<Event> list = await _collection.Find(filter).ToListAsync();
return await Task<List<Event>>.FromResult(list);
}
Thanks for all the help in advance!
Edit: I found that when I return string from the function instead of Event the whole thing works.
What I think is making problems is the ID property in the Event.
The problem was that the Event had an ObjecId property. The JSON.Net doesn't know about that type. See the solution here: JSON.NET cast error when serializing Mongo ObjectId

Entity Framework error(Store update, insert, or delete statement affected an unexcpeted

I am working on Entity Framework 4.0 . Here Adding control into database using AddObject() and save that suing SaveChange() methods.
But once I delete that added control and try to add again same I am getting this error again and again
Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.
I am not able to add it. Once I close my application then try to add then I am able to add that control.
I tried to search a lot here and there how it going wrong but could not find solution.
As I am new born in field in Entity Framework.
public void Add(SearchPath entity) {
_context.SearchPaths.AddObject(entity);
// _context.Save(entity, false);
}
public void Remove(SearchPath entity)
{
if (entity.Path != null)
{
using (EntityContext entityObj = new EntityContext())
{
entityObj.SearchPaths.Attach(entity);
entityObj.SearchPaths.DeleteObject(entity);
entityObj.SaveChanges();
}
}
}
public void Modify(SearchPath entity)
{
using (EntityContext entityObj = new EntityContext())
{
try
{
entityObj.SearchPaths.Attach(entity);
entityObj.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Modified);
entityObj.SaveChanges();
}
catch (OptimisticConcurrencyException)
{
entityObj.Refresh(RefreshMode.StoreWins, entity);
entityObj.SaveChanges();
}
}
}
public void Add(Package entity)
{
_context.Packages.AddObject(entity);
}
public void Remove(Package entity)
{
if (_context.GetEntityState(entity) == EntityState.Unchanged)
_context.Packages.Attach(entity);
_context.Packages.DeleteObject(entity);
}
Answer for above problem is Just call your own save method like this.
public void Save(object entity)
{
using (var transaction = Connection.BeginTransaction())
{
try
{
SaveChanges();
transaction.Commit();
}
catch (OptimisticConcurrencyException)
{
if (ObjectStateManager.GetObjectStateEntry(entity).State == EntityState.Deleted || ObjectStateManager.GetObjectStateEntry(entity).State == EntityState.Modified)
this.Refresh(RefreshMode.StoreWins, entity);
else if (ObjectStateManager.GetObjectStateEntry(entity).State == EntityState.Added)
Detach(entity);
AcceptAllChanges();
transaction.Commit();
}
}
}
once I delete that added control and try to add again
The problem is that you apparently do that in the same context. To me that indicates that the context's lifespan is too long. The best way to use context instances in EF is one context per unit of work (or business transaction, or use case).
So if you want to delete a control: create a context, delete the the control, dispose the context. If you want to add it again (for some reason): create a context, add the control, dispose the context. From your code snippets it is not entirely clear to me if this happens in your code.

How to log changed values to database on all action?

The controller above has a standard edit ActionResult. I simply find rows in a database by ID and update it. Before db.SaveChanges() there is log.Save() static function that saves all changes in model to separate tables in the database.It simply check old and new values from ChangeTracker.
The problem is, i want use log.Save() after db.SaveChanges(), not before, to be sure that data was really saved.
But after, in the ChangeTracker there aren't any changes so log.Save() doesn't have anything to save.
Controller:
[HttpPost]
public ActionResult edit(int id, MyModel model)
{
var hihi = db.MyModel.First(s => s.ID == model.ID);
hihi.col1 = model.col1;
hihi.col2 = model.col2;
...
log.Save(Log.ChangeType.Edit, db, id);
^ Here i save changes to log.
db.SaveChanges();
return RedirectToAction("Index");
}
Log Class:
public void Save(ChangeType changeType, DBContext parentContext, int id)
{
DBContext db = new DBContext();
foreach (System.Data.Entity.Infrastructure.DbEntityEntry ee in parentContext.ChangeTracker.Entries())
{
foreach (string column in ee.OriginalValues.PropertyNames)
{
string oldValue = ee.OriginalValues[column].ToString();
string newValue = ee.CurrentValues[column].ToString();
if (oldValue != newValue)
{
var model = new LogModel
{
Log_Time = DateTime.Now,
Log_Operator = User.Ope_ID,
Log_Table = ee.Entity.ToString().Replace("xxx.Models.", ""),
Log_Key = id,
Log_Column = column,
Log_Type = (int)changeType,
Log_OldValue = oldValue,
Log_NewValue = newValue
};
var log = db.Log.Add(model);
db.SaveChanges();
}
}
}
}
public enum ChangeType
{
Create = 1,
Delete = 2,
Edit = 3
}
... or maybe someone has another way to save all changes in a database to another table on all controller actions, so after the project release I can see what users do.
PS. I don't what user triggers.
SaveChanges in EF4 is virtual, so you can override it, add custom logging etc.
Why don't you use try{} catch{} within Log Class and change the return parameter from 'void' to 'bool'. This would return true if the db.SaveChanges() succeeds. Then within "ActionResult edit" use bool result = log.Save(Log.ChangeType.Edit, db, id); to retrieve if the log saved the changes, then use a simple if-sentence to validate if you can save all changes to db or not.

Requiring an Id from SaveChanges, whilst saving? Entity Framework

I am inserting a record into the database, which looks like this:
class Transaction
{
int Id;
}
What I want, is when I insert this object, I want to create another record, like this:
class TransactionUpdate
{
int StartingTransactionId;
int EndingTransactionId;
}
What I have so far, is a loop in my SaveChanges on the DbContext, which takes new Transaction objects that will be created and creates TransationUpdate objects and attaches these to the DbContext.
public override int SaveChanges()
{
foreach(var entry in this.ChangeTracker.Entries())
{
if(entry.Entity is Transaction)
{
var update = new TransactionUpdate();
update.StartingTransactionId = ((Transaction)entry.Entity).PreviousTransactionId;
update.EndingTransactionId = ((Transaction)entry.Entity).Id; // This is zero because the entity has not been inserted.
this.TransactionUpdates.Add(update);
}
}
}
The problem is, I cannot properly create a TransactionUpdate because I do not have 'EndingTransactionId', or, the Id of the Transaction I am currently inserting.
How can I solve this problem?
Many Thanks.
SOLVED
I have done what Ladislav suggested and am now creating a list of items to add, along with references to the objects that are required to insert them. Thus:
public override int SaveChanges()
{
var transactionUpdatesToAdd = new List<Tuple<TransactionUpdate, Transaction>>();
foreach (var entry in this.ChangeTracker.Entries<Transaction>())
{
if (entry.State == EntityState.Added)
{
var update = new TransactionUpdate();
update.StartingTransactionId = ((Transaction)entry.Entity).PreviousTransactionId;
transactionUpdatesToAdd.Add(new Tuple<TransactionUpdate, Transaction>(update, entry.Entity));
}
}
using(var scope = new TransactionScope())
{
// Save new Transactions
base.SaveChanges();
// Update TransactionUpdates with new IDs
foreach (var updateData in transactionUpdatesToAdd)
{
updateData.Item1.EndingTransactionId = updateData.Item2.Id;
this.TransactionUpdates.Add(updateData.Item1);
}
// Insert the new TransactionUpdate entities.
return base.SaveChanges();
}
Based on your description I guess you are using autogenerated Id in database. You will not receive this Id befere executing SaveChanges on the context. You have to divide operation into two separate modifications:
public override int SaveChanges()
{
// call base context saving operation to insert all Transactions
base.SaveChanges();
foreach(var entry in this.ChangeTracker.Entries())
{
if(entry.Entity is Transaction)
{
var update = new TransactionUpdate();
update.StartingTransactionId = ((Transaction)entry.Entity).PreviousTransactionId;
update.EndingTransactionId = ((Transaction)entry.Entity).Id;
this.TransactionUpdates.Add(update);
}
}
// save changes again to insert all TransactionUpdates
base.SaveChanges();
}
You should wrap it into TransactionScope to perform whole saving as atomic operation.
If you haven't inserted TransactionId, you have it anyway in your object. Pass your object as parameter to an overloaded method SaveChanges and use it to pass the Id

ApplyCurrentValues does not seem to work

I try to do the following with entity framework 4 :
public void Update(Site entity)
{
using (db)
{
db.Sites.Attach(db.Sites.Single(s => s.Id == entity.Id));
db.Sites.ApplyCurrentValues(entity);
db.SaveChanges();
}
}
But when i try to update a site through this method i get an error telling me that :
The conversion of a datetime2 data
type to a datetime data type resulted
in an out-of-range value. The
statement has been terminated.
And this is because the original site for some reason is not loaded through the Attach() method.
Can someone help with this ?
/Martin
You don't need to "attach" something you are already retrieving (Ladislav is right). Once you retrieve an entity (e.g SingleOrDefault), it is "in the graph" (EF memory - so it can do optimistic concurrency).
If your trying to do an UPDATE< and the "entity" your passing through is new/detached...
Try the stub technique:
public void Update(Site entity)
{
using (db)
{
var stub = new Site { Id = entity.Id }; // create stub with given key
db.Sites.Attach(stub); // stub is now in graph
db.Sites.ApplyCurrentValues(entity); // override graph (stub) with entity
db.SaveChanges();
}
}
That being said, the error you have provided points to some other issue (data conversion).
Have you checked the "date" values you are passing through with the data type on the model?
public ActionResult Edit(int id, Client collection)
{
try
{
// make sure the rec is in the context
var rec = dbEntities.Clients.First(r => r.ClientID == id);
// update the rec in the context with the parm values
dbEntities.Clients.ApplyCurrentValues(collection);
// make the changes permanent
dbEntities.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}