更新依赖于其他列(实体框架)的列的最佳方法是什么? [英] What is the best way to update columns that depend on other columns (entity framework)?

查看:110
本文介绍了更新依赖于其他列(实体框架)的列的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用带有MVVM模式和实体框架的WPF来访问SQL数据库中的数据。

我使用Entity Framework对数据库进行反向工程从中获取实体。

现在我需要更新Table_A上的一些数据,这些数据有30列,但我只需要用户能够更新Column10。问题是Column12依赖于Column10(Column12 = Column10 + Column11)。



1 - 什么是最有效的方式?我是否必须使用LINQ查询从表A中检索所有列?我应该在.edmx设计师中创建一个新的实体吗?我确实需要将结果作为ObservableColletion,因此创建匿名查询有点不合适..

2 - 我如何以及在何处编写更新Column12的逻辑?



我尝试过:



从数据库中获取所有列,但效率不高..

Hi,
I'm using WPF with the MVVM pattern and Entity Framework to access data from a SQL database.
I use Entity Framework to reverse engineer the database and get entities from it.
Now I need to update some data on Table_A, which has 30 columns, but I only need the user to be able to update Column10. The problem is that Column12 depends on Column10 (Column12 = Column10 + Column11).

1 - What's the most efficient way to go? Do I have to retrieve all the columns from table A using a LINQ query? Should I create myself a new Entity in the .edmx designer? I do need the result to be an ObservableColletion so creating Anonymous queries is somewhat out of the picture..
2 - How and where would I code the piece of logic to update Column12?

What I have tried:

Getting all columns from the database, but it's not efficient..

推荐答案

更新所选字段:



您可以添加仅包含所需列的新实体。确保此部分实体不用于添加新记录,除非所有其他列都是可选的。我可以想象这个实体有一个键和三列(10,11和12),你改变column10并计算column12。



替代方案如下所述:< a href =http://stackoverflow.com/questions/3642371/how-to-update-only-one-field-using-entity-framework> sql - 如何使用Entity Framework仅更新一个字段? - 堆栈溢出 [ ^ ]



栏12:



由于entity是您可以返回计算值而不是当前值的属性。不要删除集。我必须承认我没有对此进行测试,但这应该可行。

Update selected fields:

You can add a new entity with only the required columns. Make sure that this partial entity is not used to add new records, unless all other columns are optional. I can imagine this entity has a key and three columns (10, 11 and 12), where you alter column10 and calculate column12.

Alternative is as described here: sql - How to update only one field using Entity Framework? - Stack Overflow[^]

Column12:

Since the fields in the entity are properties you can return the calculated value instead of the current value. Do not remove set. I must admit that I didn't test this, but this should work.
public string colum12 { get { return column10 + column11;} set {} }



但为什么还要在代码中更新它?可以在数据库中完成同样的事情。我建议您将column12更改为计算列:在表中指定计算列 [ ^ ]


这篇关于更新依赖于其他列(实体框架)的列的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆