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 6854663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:39:41+00:00 2026-05-27T01:39:41+00:00

This is my first experience with EF so I’m probably doing something stupid. Any

  • 0

This is my first experience with EF so I’m probably doing something stupid. Any comments on the architecture are welcome.

So I have the typical class of Users. Users have a username and a list of roles:

 public class User
 {
     public string UserID{ get; set; }
     public List<Role> Roles { get; set; }
     public int Id { get; set; }
     public User()
     {
         Roles = new List<Role>();
     }
}

My domain objects live in their own code library along with the interfaces for their repositories. So in this case there would be an IUserRepository with all the CRUD methods plus any specialized data access methods I might need. What I’m trying to do is implement these repository interfaces with EF4 in another class library. Any problems with this design so far?

Now in the db (sql server) I have the typical tables: Users, Roles, and a many-to-many table mapping users to roles UsersRoles.

I have successfully set up most of the CRUD methods in the EF lib. Here is what Save looks like

public void Save(Drc.Domain.Entities.Staff.User member)
    {
        using (var ctx = new DrcDataContext())
        {
            var efUser = MapFromDomainObject(member);
            if(member.Id < 1)
            {                    
                ctx.Users.AddObject(efUser);                    
            }
            else
            {                             
                ctx.Users.Attach(efUser);
                ctx.ObjectStateManager.ChangeObjectState(efUser, EntityState.Modified);
            }          
            ctx.SaveChanges();
            member.Id = efUser.UserId;                
        }            
    }

Now I’m not sure if this is the proper way of accomplishing this but it works. However, I run into problems when doing a delete. The problem is with the related tables

public void Delete(Drc.Domain.Entities.Staff.User member)
    {
        using (var ctx = new DrcDataContext())
        {
            var efUser = MapFromDomainObject(member);    ctx.Users.Attach(efUser);
            while (efUser.Roles.Count > 0)
            {
                ctx.ObjectStateManager.ChangeObjectState(efUser.Roles.First(), EntityState.Deleted);
            }                        
            ctx.SaveChanges();
            ctx.ObjectStateManager.ChangeObjectState(efUser, EntityState.Deleted);
            ctx.SaveChanges();
        }
    }

If I don’t delete the roles in the while loop I get a DELETE conflict with reference constraint error. If I run the code above it does delete the proper rows in the many-to-many table but it also deletes rows in the Roles table. I’m at a dead end now and considering scraping the ORM idea and writing my repository implementations in good ole reliable ADO.net.

–Edit I’m guessing that this is not the correct way to implement repositories with EF. Is it possible to do without littering your domain with a bunch of EF-centric stuff?

  • 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-05-27T01:39:42+00:00Added an answer on May 27, 2026 at 1:39 am

    Use simply the standard approach and don’t mess around with the entity state:

    public void Delete(Drc.Domain.Entities.Staff.User member)
    {
        using (var ctx = new DrcDataContext())
        {
            var efUser = MapFromDomainObject(member);
            ctx.Users.Attach(efUser);
            ctx.Users.DeleteObject(efUser);
            ctx.SaveChanges();
        }
    }
    

    There is usually a cascading delete in the database from the User table to the join table (if you didn’t disable it by hand). So deleting the user will delete the corresponding rows in the join table as well (but not the roles of course).

    Setting the state of an entity to Deleted is not the same as calling DeleteObject. It will only set the parent to deleted and leave the children in an undeleted state in the context, leading to the constraint violation exception. DeleteObject will also mark the children in the context as Deleted and therefore avoid the exception.

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

Sidebar

Related Questions

Using Dozer to map two objects, I have: /** /* This first class uses
This is my first experience using the Zend Framework. I am attempting to follow
This is my first experience with javascript, and... Well... Ugh. Here's what's happening: function
#include <iostream> using namespace std; // This first class contains a vector and a
I recently installed Microsoft Test Manager 2010 and this is my first experience with
I'm developing a web system using regular PHP. This was my first experience with
This is my first experience in programming with Python and I'm trying to log
This is my first experience with themes. I've created a skin file for a
This is my first experience with C# and part of my limited experience with
I'm developing a game for android and this is my first experience with OpenGL.

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.