实体框架,将列更改为只读
我正在使用 .NET 4.0、Entity Framework 4 和 SQL Server 2005。
我有一个现有的 EF 模型。 名为 Order 的实体之一有一个名为 Name (varchar 255) 的列,该列上有一个唯一键。
过去,此列中的值是由最终用户在 Web 表单上提供其值来确定的。在提交表单之前,会与数据库中的其他值进行检查,以确保值是唯一的。
要求已更改,因此现在该列的值将在第一次创建订单时计算,此后不再更改。该计算涉及对具有相同值的字段 VariableNumber (varchar 255) 的现有订单进行计数。例如:
int count = this.Orders.Where(o => o.VariableNumber == variableNumber).Count();
++count;
return string.Format("{0}-{1:000}", variableNumber, count);
我的问题是:我应该在哪里放置此逻辑以确保首次创建订单时计算名称?
谢谢。
I am using .NET 4.0, Entity Framework 4, and SQL Server 2005.
I have an existing EF model.
One of the Entities named Order has a column named Name (varchar 255) which has a unique key on it.
In the past the value in this column was determined by end users providing its value on a web form. The value was checked against others in the database to ensure a unique value before the form could be submitted.
Requirements have changed so that now this column's value is to be calculated the first time the Order is created, and never changed afterwards. The calculation involves counting the number of existing Orders that have a field VariableNumber (varchar 255) with the same value. For Example:
int count = this.Orders.Where(o => o.VariableNumber == variableNumber).Count();
++count;
return string.Format("{0}-{1:000}", variableNumber, count);
My question is this: where do I put this logic to ensure the Name is calculated when the Order is first created?
Thank You.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种方法是在数据库触发器中执行此操作。另一种方法是在重写的 SaveChanges 中执行此操作:
One approach is doing this in a database trigger. Another approach is doing this in overriden
SaveChanges
: