简体   繁体   English

使用Entity Framework提交对数据库的更改

[英]Committing changes to the database using Entity Framework

I have a situation where I pull data from a table by date. 我有一种情况,我按日期从表中提取数据。 If no data is supplied for a given date I create a record using default values and display it all to the user. 如果没有为给定日期提供数据,我使用默认值创建记录并将其全部显示给用户。 When the user is done manipulating the data I need to commit the changes. 当用户完成操作数据时,我需要提交更改。

So my question is how do I handle in Entity Framework submitting a table where there could be both updates and adds that need to be done. 所以我的问题是如何在实体框架中处理提交表格,其中可能存在需要完成的更新和添加。 This is in C# using MVC3 and Entity Framework. 这是在使用MVC3和实体框架的C#中。

So here's what the data might look like to start, 所以这是数据可能开始的样子,

Table A 表A.

NAME  AGE PHONE_NUM 
Jim   25  555-555-5555 
Jill  48  555-551-5555

After the users done with the data it could look like this, 用户完成数据后,它可能看起来像这样,

Table A 表A.

NAME  AGE PHONE_NUM
Jim   25  555-555-5555
Jill  28  555-551-5555
Rob   42  555-534-6677

How do I commit these changes? 我如何提交这些更改? My problem is there are both updates and inserts needed? 我的问题是需要更新和插入吗?

I've found some code like this but I don't know if it will work in this case. 我发现了一些这样的代码,但我不知道它是否适用于这种情况。

For adding rows of data 用于添加数据行

entities.TABlEA.AddObject(TableOBJECT);
entities.SaveChanges();

or for updating data 或者用于更新数据

entities.TABLEA.Attach(entities.TABLEA.Single(t => t.NAME == TableOBJECT.NAME));
entities.TABLEA.ApplyCurrentValues(TableOBJECT);
entities.SaveChanges();

Will any of this work or do I need to keep track of whats there and what was added? 是否有任何这项工作或我是否需要跟踪那里有什么以及添加了什么?

Ideas? 想法?

More or less you already have the solution. 或多或少你已经有了解决方案。 You just need to check if your Single call which tries to load the object from the DB has an result or not (use SingleOrDefault instead). 您只需要检查尝试从数据库加载对象的Single调用是否有结果(改为使用SingleOrDefault )。 If the result is null you need to insert, otherwise update: 如果结果为null ,则需要插入,否则更新:

foreach (var TableOBJECT in collectionOfYourTableOBJECTsTheUserWorkedWith)
{
    var objectInDB = entities.TABLEA
        .SingleOrDefault(t => t.NAME == TableOBJECT.NAME);
    if (objectInDB != null) // UPDATE
        entities.TABLEA.ApplyCurrentValues(TableOBJECT);
    else // INSERT
        entities.TABLEA.AddObject(TableOBJECT);
}
entities.SaveChanges();

(I'm assuming that NAME is the primary key property of your TableOBJECT entity.) (我假设NAME是您的TableOBJECT实体的主要键属性。)

I think you have to keep track of what is new and what is modified. 我认为你必须跟踪新内容和修改内容。 If you do that, that the two code examples you provided are going to work. 如果您这样做,那么您提供的两个代码示例将起作用。 A simple workaround which I used is to check if an entity's primary key property is set to anything. 我使用的一个简单的解决方法是检查实体的主键属性是否设置为任何东西。 If it is set to a value, then that is an updated object, otherwise it's new. 如果它被设置为一个值,那么这是一个更新的对象,否则它是新的。

Another solution would be to use Entity Framework's Self Tracking Entities, but I do not think that's the right direction to go in a web application (maybe it is in a distributed WCF app). 另一种解决方案是使用Entity Framework的自我跟踪实体,但我认为这不是进入Web应用程序的正确方向(可能是在分布式WCF应用程序中)。

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

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