自追踪 poco

发布于 2024-11-02 17:55:45 字数 589 浏览 1 评论 0原文

我有一个像这样的 poco 类

public Profile
{
    public virtual int ID
    {
        get;
        set;
    }

    public virtual string Description
    {
        get;
        set;
    }

    public virtual bool Enabled
    {
        get;
        set;
    }
}

当我尝试像这样更新时

var prof = new Profile(){ ID = 1, Enabled = false };
context.Profiles.Single (s => s.ID == 1);
context.Profiles.ApplyCurrentValues(prof);
context.SaveChanges();

Sql 对我说 Description 不允许 NULL,但我没有更新“Description”列,我只想更新“Enabled”字段。

怎么了?

塔克斯

I have a poco class like this

public Profile
{
    public virtual int ID
    {
        get;
        set;
    }

    public virtual string Description
    {
        get;
        set;
    }

    public virtual bool Enabled
    {
        get;
        set;
    }
}

When i try to update like this

var prof = new Profile(){ ID = 1, Enabled = false };
context.Profiles.Single (s => s.ID == 1);
context.Profiles.ApplyCurrentValues(prof);
context.SaveChanges();

Sql says to me that Description does not allow NULL, but i'm not updating the "Description" column, I want to update just the "Enabled" field.

What's wrong?

Tks

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

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

发布评论

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

评论(3

临风闻羌笛 2024-11-09 17:55:45

请尝试以下操作:

var prof = new Profile { ID = 1, Enabled = false };
// Attach prof as unchanged entity
context.Profiles.Attach(prof);
// Get state entry for prof
ObjectStateEntry entry = context.ObjectStateManager.GetObjectStateEntry(prof);
// Set only Enabled property to changed
entry.SetModifiedProperty("Enabled");
context.SaveChanges();

如您所见,我不需要先从数据库加载实体。我能够设置在分离实体上修改哪个属性。

Try this instead:

var prof = new Profile { ID = 1, Enabled = false };
// Attach prof as unchanged entity
context.Profiles.Attach(prof);
// Get state entry for prof
ObjectStateEntry entry = context.ObjectStateManager.GetObjectStateEntry(prof);
// Set only Enabled property to changed
entry.SetModifiedProperty("Enabled");
context.SaveChanges();

As you can see I didn't need to load the entity first from database. I was able to set which property was modified on detached entity.

假面具 2024-11-09 17:55:45

根据我的评论:在不了解 EF4 的情况下,我希望它的工作方式如下:

var prof = context.Profiles.Single (s => s.ID == 1);
prof.Enabled = false;
context.Profiles.ApplyCurrentValues(prof);
context.SaveChanges();

假设 Single 是 Linq Single,那么它将返回一个您应该修改的值。

Based on my comment: Without knowing EF4, this is how I'd expect it to work:

var prof = context.Profiles.Single (s => s.ID == 1);
prof.Enabled = false;
context.Profiles.ApplyCurrentValues(prof);
context.SaveChanges();

Assuming Single is the Linq Single, then it will return a value that you should modify.

情深如许 2024-11-09 17:55:45

这不行吗?

var prof = context.Profiles.Single(s => s.ID == 1);
prof.Enabled = false;
context.SaveChanges(); 

我认为问题在于您从未跟踪的对象调用ApplyCurrentValues - 这意味着它不知道哪些属性已更改和未更改,因此它只是复制所有属性,包括那些为空的 - 在你的情况描述中。

如果要修改从数据上下文加载的实体的值,则无需调用ApplyCurrentValues。

Does this not work?

var prof = context.Profiles.Single(s => s.ID == 1);
prof.Enabled = false;
context.SaveChanges(); 

I think the problem is that you're calling ApplyCurrentValues from an untracked object - this means that it has no idea which properties have and haven't been changed, so it just copies across all the properties, including the ones that are null - in your case Description.

You don't need to call ApplyCurrentValues if you are modifying the values of an entity that is loaded from the data context.

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