按照 NerdDinner 教程,不允许重复

发布于 2024-08-24 02:47:24 字数 494 浏览 4 评论 0原文

我的sql数据库不允许添加2条具有相同编号的记录。如果我尝试使用以前使用过的号码创建记录或编辑记录以使用以前使用过的号码,则它不允许这样做,并返回到编辑/创建页面,并在分包合同号码字段旁边带有星号。我想为此添加一个规则违规,以便可以显示适当的消息。我尝试添加以下内容:

 if (db.subcontracts.Count(s => s.subcontract_no == subcontract_no) > 0)
      yield return new RuleViolation("Subcontract number already exists", "subcontract_no");

在编写此内容时,我只想到 Create 方法。这里的问题是,每次我编辑记录时,即使 subcontract_no 没有更改,它也会发现违规。因此,我想我需要它来在创建记录时以及在编辑 subcontract_no 更改时查找违规情况。但是,它无法“找到自己”并造成违反规则的情况。我该怎么做?

My sql database doesn't allow 2 records to be added with the same number. If I try to create a record with a previously used number or edit a record to use a previously used number, it doesn't allow it and returns to the edit/create page with an asterisk by the subcontract number field. I would like to add a Rule Violation for this so an appropriate message can be displayed. I tried adding this:

 if (db.subcontracts.Count(s => s.subcontract_no == subcontract_no) > 0)
      yield return new RuleViolation("Subcontract number already exists", "subcontract_no");

When writing this, I was only thinking of the Create method. The problem here is it finds a violation every time I edit a record, even if the subcontract_no hasn't changed. So, I guess I need it to find a violation when the record is created and when edited if the subcontract_no is changed. But, it can't "find itself" and create a rule violation. How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜光 2024-08-31 02:47:24

尝试:

db.subcontracts.Count(s => (s.subcontract_no == subcontract_no) 
    && (s.id != actually_edited_or_created_contract.id)).Count

即使entity.id == null,它也适用于新的或现有的实体。

Try:

db.subcontracts.Count(s => (s.subcontract_no == subcontract_no) 
    && (s.id != actually_edited_or_created_contract.id)).Count

It will work with new or existing entity, even when entity.id == null.

九厘米的零° 2024-08-31 02:47:24

首先,在编辑/更新页面上,如果该值无法更改,则不要将其显示为可编辑值。您可能希望在操作中取回 ViewModel,从数据库中检索相应的模型,并使用 TryUpdateModel 并限制排除不可变字段。其次,在验证代码中,当更改类型为“更新”(而不是“插入”)时,省略对重复分包合同编号的检查。使用一些代码来增强此功能,这些代码与分包编号属性的属性更改处理程序相关联,如果分包编号已经具有(非初始)值并且您尝试更改它,则该处理程序将引发异常。这应该可以防止在插入时以外的情况下设置该值,并允许您跳过更新时的验证检查。

您还可以将其与强制列唯一性的数据库约束结合起来——非空列上的唯一索引将起作用。

First, on your edit/update page, if the value can't be changed, don't display it as an editable value. You might want to get back a ViewModel in the action, retrieve the corresponding model from the DB, and use TryUpdateModel with a restriction that excludes the immutable fields. Second, in your validation code, omit the check for a duplicate subcontract number when the change type is Update (instead of Insert). Augment this with a bit of code that ties into the property changing handler for the subcontract number property that throws an exception if the subcontract number already has a (non-initial) value and you attempt to change it. This should prevent the value from being set except at insertion and allow you to skip the validation check on update.

You could also couple this with a DB constraint that forces uniqueness on the column -- a unique index on a non-null column would work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文