EF 代码优先 - 无法更新数据库行

发布于 2024-09-30 12:17:27 字数 1081 浏览 3 评论 0原文

正在尝试使用 EF“代码优先”方法编写一些代码,但遇到了一个奇怪的问题。

我的数据上下文:

    public class BookmarkerDataContext : DbContext
    {
        public DbSet<User> Users { get; set; }
        protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<User>().HasKey(u => u.UserId);
            base.OnModelCreating(modelBuilder);
        }
     }

用户对象在哪里:

   public class User
    {
        public long UserId { get; set; }
        public ICollection<Tag> Tags { get; set; }
    }

在我的代码中,我正在做一些相当简单的事情:

public void UpdateUserTags(User user,ICollection<Tag> taglist)
    {
        user.Tags = new List<Tag>(user.Tags.Union(taglist));
        datacontext.Users.Add(user);
        datacontext.SaveChanges();
    }

我传递给此函数的用户对象是类似以下内容的结果:

datacontext.Users.SingleOrDefault(u => u.UserId==id)

每次我调用 UpdateUserTags 函数时,它似乎都会在用户表而不是更新它。我在这里做错了什么吗?

Was trying out some code with the EF "code first" method and ran into a strange problem.

My datacontext:

    public class BookmarkerDataContext : DbContext
    {
        public DbSet<User> Users { get; set; }
        protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<User>().HasKey(u => u.UserId);
            base.OnModelCreating(modelBuilder);
        }
     }

Where the user object is:

   public class User
    {
        public long UserId { get; set; }
        public ICollection<Tag> Tags { get; set; }
    }

In my code I am doing something fairly simple:

public void UpdateUserTags(User user,ICollection<Tag> taglist)
    {
        user.Tags = new List<Tag>(user.Tags.Union(taglist));
        datacontext.Users.Add(user);
        datacontext.SaveChanges();
    }

The user object I am passing to this function is the result of something like:

datacontext.Users.SingleOrDefault(u => u.UserId==id)

Everytime I call the UpdateUserTags function it seems to create a new Row in the User table instead of updating it. Am I doing something wrong here?

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

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

发布评论

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

评论(4

活泼老夫 2024-10-07 12:17:27

@Donald 是正确的,您在进行更新时需要Attach 到ObjectContext。

然而,这只适用于你的实体是分离的。

如果听起来您已经从图表中检索了单个实体:

var user = datacontext.Users.SingleOrDefault(u => u.UserId==id);

这意味着您不需要再次附加或获取它。只需这样做:

var user = datacontext.Users.SingleOrDefault(u => u.UserId==id);
user.Tags = new List<Tag>(user.Tags.Union(taglist));
context.SaveChanges();

但是,我不建议替换整个标签集合,添加标签:

user.Tags.Add(someTag);

HTH

@Donald is correct, you need to Attach to the ObjectContext when making updates.

However, that is only if your entity is detached.

If sounds like you have already retrieved the single entity from the graph:

var user = datacontext.Users.SingleOrDefault(u => u.UserId==id);

Which means you don't need to Attach or get it again. Just do this:

var user = datacontext.Users.SingleOrDefault(u => u.UserId==id);
user.Tags = new List<Tag>(user.Tags.Union(taglist));
context.SaveChanges();

However, i wouldn't recommend replacing the entire Tags collection, add the tags:

user.Tags.Add(someTag);

HTH

我做我的改变 2024-10-07 12:17:27

我相信您想要附加您的对象到数据上下文,而不是添加它。

public void UpdateUserTags(User user,ICollection<Tag> taglist)
{
    datacontext.Attach(user);
    user.Tags = new List<Tag>(user.Tags.Union(taglist));
    datacontext.SaveChanges();
}

一旦它被附加,上下文就会意识到该对象。保存更改后,它们应该保存到数据库中。

I believe you want to Attach your object to the data context, instead of Adding it.

public void UpdateUserTags(User user,ICollection<Tag> taglist)
{
    datacontext.Attach(user);
    user.Tags = new List<Tag>(user.Tags.Union(taglist));
    datacontext.SaveChanges();
}

Once it is attached, then the context becomes aware of the object. Once you save changes, they should be persisted to the database.

一人独醉 2024-10-07 12:17:27

这行不会

datacontext.Users.Add(user);

始终将用户记录标记为需要添加到用户表中。

我认为您必须将用户从旧上下文中分离出来并将其附加到新上下文中才能正确更新记录。
但我不是 EF 迷,所以 ymmv

Won't this line

datacontext.Users.Add(user);

Always mark the user record as needing to be ADDED to the users table.

I think you have to DETACH the user from the old context and ATTACH it to the new to properly be able to update the record.
but I'm no EF wonk so ymmv

朱染 2024-10-07 12:17:27

这与您的特定问题无关,但对于延迟加载,您需要将标签标记为虚拟

public virtual ICollection<Tag> Tags { get; set; }

此外,看起来您正在尝试为用户添加新标签(或更新他们的标签),如果这是如果是这种情况,那么您将需要按照唐纳德的建议使用 Attach

This has nothing to do with your particular issue but for lazy loading you're going to want Tags to be marked virtual

public virtual ICollection<Tag> Tags { get; set; }

Also, it looks like you're trying to add new tags for a user (or update their tags) if that is the case then you're going to want to use Attach as Donald suggested

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