gpt4 book ai didi

.net - 实现计算列的最佳方法是什么?

转载 作者:行者123 更新时间:2023-12-03 17:00:36 25 4
gpt4 key购买 nike

我正在使用 Entity Framework 6 和数据库优先方法来构建项目,
假设这两个表:

Tasks
id | taskName | taskPrice
1 | Making UI| 100
2 | Debugging| 70

和第二个表:

Employees
id | Name | spentHours| taskId | *totalPrice*
1 | Iman | 10 | 2 | (10 * 70) = 700
2 | Sam | 5 | 1 | (5 * 100) = 500

我需要使 totalPrice 值可用于我项目的所有部分,因此最好不要为每次使用都计算它。
为了实现这一点,到目前为止,我已经找到了这三种解决方案:1- 我可以为 totalPrice 分配一列并更新它的值,我认为这不是最优化的方法,因为如果 taskPrice 发生变化,我将需要重新计算并更新 totalPrice 值。

第二种方法是使用存储过程。

3- 第三个是使用触发器。

哪种方法在计算、开销和时间方面最优化?
如果有任何其他方法可以这样做,请告诉我。

最佳答案

您可以在 DDL 中有一个计算列。创建表表名(身份证,名称 varchar(20)花费数小时,任务标识符,fn_totalprice(spenthours, id) as totalprcie)

函数可以是create function fn_totalprice spenthours int, id int作为( SELECT (e.spenthours*t.taskprice) 作为总价 来自员工 e e.@id=t.id 上的内部连接任务 t 返回总价)

关于.net - 实现计算列的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30680137/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com