문제

I see two codes for adding and deleting entity, I wonder which is the best way and what is the difference between these two. One is this (for Adding):

using (var context = new BloggingContext()) 
{ 
    var blog = new Blog { Name = "ADO.NET Blog" }; 
    context.Blogs.Add(blog); 
    context.SaveChanges(); 
}

and another is this:

using (var context = new BloggingContext()) 
{ 
    var blog = new Blog { Name = "ADO.NET Blog" }; 
    context.Entry(blog).State = EntityState.Added; 
    context.SaveChanges(); 
}

and I read that calling the Add method on DbSet puts the entity into the Added state.

According to this I think above two codes are nearly same. If its not please tell me the difference.

And another code I've found is:

public virtual void Add(T entity)
{
    DbEntityEntry dbEntityEntry = DbContext.Entry(entity);
    if (dbEntityEntry.State != EntityState.Added)
    {
        dbEntityEntry.State = EntityState.Added;
    }
    else
    {
        DbSet.Add(entity);
    }
}

and if its true that calling the Add method on DbSet puts the entity into the Added state then I think there is no difference in code in if and else block, so what's the point in here.

And from the above three code which is the best way to add an Entity.

And another code in which I have doubt is what is the use of else block in below code:

public virtual void Delete(T entity)
{
    DbEntityEntry dbEntityEntry = DbContext.Entry(entity);
    if (dbEntityEntry.State != EntityState.Deleted)
    {
        dbEntityEntry.State = EntityState.Deleted;
    }
    else
    {
        DbSet.Attach(entity);
        DbSet.Remove(entity);
    }
}
도움이 되었습니까?

해결책

I don't see a huge benefit in setting the state of an entity to Added since creating a new entity and adding it to the set does exactly that like you mentioned. Where this type of pattern is pretty useful is when you want to delete an entity without having to fetch it from the database first:

// this entity will be unattached at this point, so if you were to call remove
// on the dbset it wouldn't do much, since it doesn't think it's in the database
var deleteThisEntity = new Blog { Id = 5 };

// if you set the state to deleted, it now thinks that it needs to be deleted
db.Entry(deleteThisEntity).State = EntityState.Deleted;

// call SaveChanges to delete
db.SaveChanges();

You can get a similar effect by setting the state to modified, so it will trigger an update statement. Sometimes you just don't want to take the extra hit of fetching an item from the database just to delete it.

다른 팁

ADD Patterns for ASP .NET using the code bellow is very standard practice.

using (var context = new BloggingContext()) 
{ 
    var blog = new Blog { Name = "ADO.NET Blog" }; 
    context.Blogs.Add(blog); 
    context.SaveChanges(); 
}

In the code for delete, the if/else statement is to check if the fetched object is valid.

The code I've been using for DELETE patterns is this:

var fetchedObject = context.Blog.Find(id);

if (fetchedObject == null)
{
    return false;
}
else
{
    ds.Blog.Remove(fetchedObject);
    return true;
}

This is a method inside a Manager class that receives the id.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top