简体   繁体   English

在Entity Framework 4.3中更新实体属性的正确方法-代码优先

[英]Proper Way to Update Properties of an Entity in Entity Framework 4.3 - Code First

What is the proper way to do an update when properties of an entity change? 当实体的属性更改时,进行更新的正确方法是什么? For example, I have an entity called Application. 例如,我有一个名为Application的实体。 Application has a property List<TaskBases>. 应用程序具有属性List <TaskBases>。 When running my app, Application has zero TaskBases. 运行我的应用程序时,应用程序的TaskBase为零。 I just need to simply know how to save this list when a user adds a TaskBase to it. 我只需要简单地知道当用户向其添加TaskBase时如何保存此列表。

The first line in the Save() method is new. Save()方法的第一行是新的。 I tried it because I found it while researching. 我尝试了它是因为我在研究时发现了它。

public void Save(Application application)
{
    // This line didn't work. See error below this code snippet.
    this.Database.Entry(application).Property(app => app.Tasks).IsModified = true;
    this.SaveChanges<Application>(application);
}

protected void SaveChanges<T>(T entity) where T : EntityBase
{
    this.Database.Entry<T>(entity).State = GetState(entity);
    this.Database.SaveChanges();
}

I get this error: "The property 'Tasks' on type 'Application' is not a primitive or complex property. The Property method can only be used with primitive or complex properties. Use the Reference or Collection method." 我收到此错误:“类型'应用程序'上的属性'任务'不是原始属性或复杂属性。属性方法只能与原始属性或复杂属性一起使用。请使用Reference或Collection方法。

So then I tried using Reference and Collection, but I don't see anything in intellisense that would help me. 因此,我尝试使用引用和集合,但是在智能感知中我看不到有什么对我有帮助的。

Note: When I first tried saving without having that first line in Save(), the item in the list of TaskBases didn't show up in the DB. 注意:当我第一次尝试在Save()中没有第一行的情况下进行保存时,TaskBases列表中的项目未显示在数据库中。

I have a feeling I'm making this harder than it needs to be. 我有种感觉,我正在使这个工作变得比原来更困难。 I have an entity, a user adds another entity to it, as part of a list, and I want that to save. 我有一个实体,用户将另一个实体添加到列表中,作为列表的一部分,我希望将其保存。 What am I missing? 我想念什么? How should this be done? 应该怎么做?

EDIT: 编辑:

When I changed my Save() method to this, it worked: 当我将Save()方法更改为此时,它起作用了:

public void Save(Application application)
{
    foreach (TaskBase taskBase in application.Tasks)
    {
        this.Database.Entry<TaskBase>(taskBase).State = GetState(taskBase);
    }

    this.SaveChanges<Application>(application);
}

Is that what has to be done? 那是必须要做的吗? Is that the right way? 那是正确的方法吗?

The normal method for performing updates goes something like this: 执行更新的常规方法如下:

using(var context = new MyDbContext())
{
    var app = context.Applications.Single(a=>a.Id == theAppIdImInterestedIn); 
    app.Tasks.Add(new Task{ Name = "some other data" });
    context.SaveChanges();
}

If you want to separate this out a bit abstract your context away using a DI container to manage its lifecycle. 如果您想将其分离出来,可以使用DI容器来管理其生命周期,从而抽象出您的上下文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM