EntityFramework更新实体与另一个的值?

Kyl*_*yle 3 c# entity-framework entity-framework-5

我有一个JSON字符串,我将其保存为EF权利.

Report result = js.Deserialize<Report>(json);
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用相同的ID更新我的上下文中的实体,以获得我的deserlized值.

var reportToUpdate    _entities.Reports.Single(x => x.Id == result.Id)
Run Code Online (Sandbox Code Playgroud)

我想做这样的事情

reportToUpdate = set all values to the values from result
context.SaveChanges();
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

我想避免做这样的事情:

report.param1 = result.param1
report.param3 = result.param3
report.param3 = result.param3
Run Code Online (Sandbox Code Playgroud)

因为这个实体有大约50个属性.

Eri*_*sch 8

这应该适合你.

context.Reports.Entry(reportToUpdate).CurrentValues.SetValues(result);
context.SaveChanges();
Run Code Online (Sandbox Code Playgroud)

请注意,SetValues不遵循导航属性或相关对象,只遵循实体本身的复杂/简单属性.