将用户添加到角色会将重复的用户插入用户表中

发布于 2024-11-04 14:01:03 字数 662 浏览 1 评论 0原文

我首先使用实体​​框架代码来为我的用户管理数据库后台存储。我有一个“将角色添加到用户”操作,该操作从数据库中提取用户,将该用户添加到角色,然后保存更改。但是,当我执行此操作时,用户的新副本将使用新的/不同的 ID(唯一键)插入到数据库中,而不是我从数据库中提取的用户,我不确定为什么。对于为什么会发生这种情况有什么想法吗?

 IEnumerable<long> usersIdsToGiveRole = from u in vm.UsersNotInSelectedRole where u.IsSelected select u.Id; // say, yields "5"
 IEnumerable<User> usersToGiveRole = _userRepository.InternalUsers.Where(u => usersIdsToGiveRole.Contains(u.ID)); // gets user with ID 5
 foreach (var user in usersToGiveRole)
 {
       selectedRole.UsersWithRole.Add(user);
 }


 _roleRepository.SaveChanges(); // creates new user with ID 6 cloning all other fields of user 5

I'm using Entity Framework code first to manage a database backstore for my users. I have an "add role to user" operation that pulls a user from the db, adds that user to the role, and then saves changes. However, when I do this a new copy of the user is inserted into the database with a new/different ID (unique key) than the user I pulled from the db and I'm not sure why. Any thoughts on why this is happening?

 IEnumerable<long> usersIdsToGiveRole = from u in vm.UsersNotInSelectedRole where u.IsSelected select u.Id; // say, yields "5"
 IEnumerable<User> usersToGiveRole = _userRepository.InternalUsers.Where(u => usersIdsToGiveRole.Contains(u.ID)); // gets user with ID 5
 foreach (var user in usersToGiveRole)
 {
       selectedRole.UsersWithRole.Add(user);
 }


 _roleRepository.SaveChanges(); // creates new user with ID 6 cloning all other fields of user 5

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嗳卜坏 2024-11-11 14:01:03

只是猜测:您似乎对 _userRepository_roleRepository 有单独的 ObjectContext。通过从附加到此上下文的 _userRepository 加载 usersToGiveRoleselectedRole 似乎附加到 _roleRepository 的其他上下文。当您将 user 添加到 selectedRole.UsersWithRole 时,您将其添加到第二个上下文中(user 现在位于 added 中) _roleRepository 上下文中的状态)。当您调用此上下文的 SaveChanges 时,现在会在数据库中创建一个新的 User 对象。

解决方案:确保在两个存储库中仅使用一个上下文。

编辑

简而言之,我的意思是:

不要这样做:

class UserRepository
{
    private readonly MyContext _context;

    public UserRepository()
    {
        _context = new MyContext();
    }

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

class RoleRepository
{
    private readonly MyContext _context;

    public RoleRepository()
    {
        _context = new MyContext();
    }

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

...

var userRepository = new UserRepository();
var roleRepository = new RoleRepository();

// CRUD

userRepository.SaveChanges();

// perhaps other CRUD

roleRepository.SaveChanges();

而是这样做:

class UserRepository
{
    private readonly MyContext _context;

    public UserRepository(MyContext context)
    {
        _context = context;
    }
}

class RoleRepository
{
    private readonly MyContext _context;

    public RoleRepository(MyContext context)
    {
        _context = context;
    }
}

...

using (var context = new MyContext())
{
    var userRepository = new UserRepository(context);
    var roleRepository = new RoleRepository(context);

    // CRUD

    context.SaveChanges();
}

上下文(或工作单元)始终是一个级别在存储库之上,应该在外部创建并注入到存储库中。

Just a guess: You seem to have separate ObjectContexts for _userRepository and for _roleRepository. By loading usersToGiveRole from the _userRepository you attach to this context. selectedRole seems to be attached to the other context of _roleRepository. When you add the user to selectedRole.UsersWithRole you add it to this second context (user is now in added state in the context of _roleRepository). When you call SaveChanges of this context now a new User object is created in the database.

Solution: Make sure that you only use one single context in both repositories.

Edit

In short what I mean:

Don't do this:

class UserRepository
{
    private readonly MyContext _context;

    public UserRepository()
    {
        _context = new MyContext();
    }

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

class RoleRepository
{
    private readonly MyContext _context;

    public RoleRepository()
    {
        _context = new MyContext();
    }

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

...

var userRepository = new UserRepository();
var roleRepository = new RoleRepository();

// CRUD

userRepository.SaveChanges();

// perhaps other CRUD

roleRepository.SaveChanges();

Instead do this:

class UserRepository
{
    private readonly MyContext _context;

    public UserRepository(MyContext context)
    {
        _context = context;
    }
}

class RoleRepository
{
    private readonly MyContext _context;

    public RoleRepository(MyContext context)
    {
        _context = context;
    }
}

...

using (var context = new MyContext())
{
    var userRepository = new UserRepository(context);
    var roleRepository = new RoleRepository(context);

    // CRUD

    context.SaveChanges();
}

The context (or Unit of Work) is always a level above the repositories, should be created outside and injected into the repos.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文