Which is better to use, DbContext.Add() or DbSet.Add()?

Yalçın Mete 40 Reputation points
2024-06-10T07:02:43.7566667+00:00

Hi,

If you allow, I would like to ask a question.

In CRUD operations, we can add to the database in two ways: DbContext.Add(object) and DbSet.Add(object). Which one is more correct to use?

public class Context : DbContext

{

public DbSet<Category> Categories { get; set; }

}

public void AddCategory(Category category)

{

c.Add(category);

c.Categories.Add(category); //Both methods work

c.SaveChanges();

}

Thanks

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
714 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,524 questions
{count} votes

Accepted answer
  1. Jack J Jun 24,476 Reputation points Microsoft Vendor
    2024-06-10T07:54:55.6133333+00:00

    @Yalçın Mete, Welcome to Microsoft Q&A, after checking the source code of the two methods, I find they are almost the same.

    DbContext.Add Source Code:

     public virtual EntityEntry Add(object entity)
            {
                CheckDisposed();
                return SetEntityState(Check.NotNull(entity, nameof(entity)), EntityState.Added);
            }
    
    
    

    DbSet.Add Source Code

     public override EntityEntry<TEntity> Add(TEntity entity)
            {
                var entry = EntryWithoutDetectChanges(entity);
                SetEntityState(entry.GetInfrastructure(), EntityState.Added);
                return entry;
            }
    

    They both used SetEntityState method to add the data.

    However, there are some notes you need to care.

    DbContext.Add can be handy when you have multiple entity types to add and you want to manage them all within the same context.

    DbSet.Add is often used when you're working directly with a particular entity set within the context.

    In most scenarios, you'll find yourself using DbSet.Add() because it's more convenient and specific, especially when you're working with a particular type of entity. However, there might be cases where using DbContext.Add() could be more appropriate, such as when you're dealing with multiple entity types within the same context. Ultimately, both methods achieve the same goal of adding entities to the context, so the choice between them depends on the specific requirements of your application and the context in which you're working.

    Hope it could help you.

    Best Regards,

    Jack


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. Yalçın Mete 40 Reputation points
    2024-06-10T08:10:23.8833333+00:00

    When I came across the two methods, I thought there must be a difference between them.

    As far as I understand, there is no difference between them.

    @Jack J Jun Thank you very much.

    0 comments No comments