简体   繁体   中英

Deleting and Inserting Entity with Entity Framework

Suppose that I have two entities E1 and E2 both share the same Code value. Suppose that I want to delete the first and insert the next:

Item.Delete(E1);
Item.Insert(E2);
Item.Save();

Where

public void Delete(Entity E) 
{
    var existingEntity = _context.EntityTable.SingleOrDefault(s => s.Code == E.Code);
    _context.EntityTable.Remove(existingEntity);
}

public void Insert(Entity E)
{
    var existingEntity = _context.EntityTable.FirstOrDefault(s => s.Code == E.Code);
    if (existingEntity != null){
        throw new ArgumentException("Item alread exists.")
    }

    var newEntity = CreateDbEntity(E); // Create Db Entity just convert the type. Nothing much here.
    _context.EntityTable.Add(newEntity);
}

public void Save()
{
    _context.SaveChanges();
}

The problem here is that when I remove E1 from the EntityTable in the _context , that is not reflected immediately until I save. Thus, the operation will fail because inserting E2 sill not be successful since E1 is still there. Is there away work around that, where EntityTable does reflect the changes that have been made?

Simple call SaveChanges after each operation.

You want to make a transaction when your initializing your context (this makes sure both operation are executed):

_context = new FooEntities();
_transaction = _context.Database.BeginTransaction();

Your Save method will then commit the transaction instead:

_transaction.Commit();

It won't hurt to Dispose the context and the transaction, but I am sure you already do it ;)


On a side note, since you throw a exception anyway:

if (existingEntity != null){
    throw new ArgumentException("Item alread exists.")
}

Why not creating a unique constraint on the Code column? This way the database throws the exception ;)

It looks like you just have an order of operations error this might not be the most efficient way but it should take care of your problem.

Item.Delete(E1);
Item.Save();
Item.Insert(E2);
Item.Save();

call them in that order.

or you can add the save method into your delete and insert methods so it saves as it performs each one and you would only have to use two lines.

public void Delete(Entity E) 
{
    var existingEntity = _context.EntityTable.SingleOrDefault(s => s.Code == E.Code);
    _context.EntityTable.Remove(existingEntity);
    Save()
}

public void Insert(Entity E)
{
    var existingEntity = _context.EntityTable.FirstOrDefault(s => s.Code == E.Code);
    if (existingEntity != null){
        throw new ArgumentException("Item alread exists.")
    }

    var newEntity = CreateDbEntity(E); // Create Db Entity just convert the type. Nothing much here.
    _context.EntityTable.Add(newEntity);
    Save()
}

public void Save()
{
    _context.SaveChanges();
}

and then you can call it like

Item.Delete(E1);
Item.Insert(E2);

Hope this helps! If not let me know and I'll remove the answer(I had to use an answer because I can't comment under 50 rep, otherwise I would have used a comment to get some more clarity before answering) Cheers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM