Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8639127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:55:17+00:00 2026-06-12T10:55:17+00:00

Note: I am using Entity Framework version 5 Inside my generic repository, I have

  • 0

Note: I am using Entity Framework version 5

Inside my generic repository, I have Add, Edit and Delete methods as below:

public class EntityRepository<T> : IEntityRepository<T>
    where T : class, IEntity, new() {

    readonly DbContext _entitiesContext;

    public EntityRepository(DbContext entitiesContext) {

        if (entitiesContext == null) {

            throw new ArgumentNullException("entitiesContext");
        }

        _entitiesContext = entitiesContext;
    }

    //...

    public virtual void Add(T entity) {

        DbEntityEntry dbEntityEntry = _entitiesContext.Entry<T>(entity);
        if (dbEntityEntry.State != EntityState.Detached) {

            dbEntityEntry.State = EntityState.Added;
        }
        else {

            _entitiesContext.Set<T>().Add(entity);
        }
    }

    public virtual void Edit(T entity) {

        DbEntityEntry dbEntityEntry = _entitiesContext.Entry<T>(entity);
        if (dbEntityEntry.State == EntityState.Detached) {

            _entitiesContext.Set<T>().Attach(entity);
        }

        dbEntityEntry.State = EntityState.Modified;
    }

    public virtual void Delete(T entity) {

        DbEntityEntry dbEntityEntry = _entitiesContext.Entry<T>(entity);
        if (dbEntityEntry.State != EntityState.Detached) {

            dbEntityEntry.State = EntityState.Deleted;
        }
        else {

            DbSet dbSet = _entitiesContext.Set<T>();
            dbSet.Attach(entity);
            dbSet.Remove(entity);
        }
    }
}

Do you think whether these methods are well implemented? Especially the Add method. Would it be better to implement the Add method as below?

public virtual void Add(T entity) {

    DbEntityEntry dbEntityEntry = _entitiesContext.Entry<T>(entity);
    if (dbEntityEntry.State == EntityState.Detached) {

        _entitiesContext.Set<T>().Attach(entity);
    }

    dbEntityEntry.State = EntityState.Added;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T10:55:19+00:00Added an answer on June 12, 2026 at 10:55 am

    For add:

    public bool Add<E>(E entity) where E : class
            {
                DataContext.Entry(entity).State = System.Data.EntityState.Added;
                Save();
            }
    

    For update:

     public bool Update<E>(E entity) where E : class
            {
                DataContext.Entry(entity).State = System.Data.EntityState.Modified;
                Save();
            }
    

    For delete:

     public bool Delete<E>(E entity) where E : class
            {
                DataContext.Entry(entity).State = System.Data.EntityState.Deleted;
                Save();
            }
    

    And a private Save() method that returns true or false so you can fallback easy in the controller depending on the result

    private bool Save()
            {
                return DataContext.SaveChanges() > 0;                
            }
    

    This is only a portion of my generic repository. It works great in enterprise applications.

    UPDATE:

    Detach only affects the specific object passed to the method. If the
    object being detached has related objects in the object context, those
    objects are not detached.

    EF will automatically attach detached objects in the graph when setting the state of an entity or when SaveChanges() is called.

    I really don’t know why you need to detach objects from the context. You can also use AsNoTracking() to load entities from the database without attaching them to the context in the first place.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

NOTE: The scenario is using 2 entity framework models to sync data between 2
My scenario I'm using Visual Studio 2010 with Entity Framework 4.1 I have a
I have an application I am working on, using Entity Framework 4.1 as my
I am using the .Skip and .Take methods with Entity Framework. The .Skip call
I have the following table created using Entity Framework Code First approach. How do
I am using Entity Framework 4 with the POCO code generator. I have a
I am using Entity framework on a forum website and have two query examples
I have a web application that is using Entity Framework to query a SQL
So I have been working on a project using Entity Framework 5 in development
I have a console application that is using code first Entity Framework 4.3.1. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.