如何在另一个表中创建新记录时修改表中的字段? [英] How to modify a field in a table when a new record is created in another table?

查看:115
本文介绍了如何在另一个表中创建新记录时修改表中的字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生,


我正在使用Entity框架在ASP.net MVC中开发一个应用程序,我有以下表格:


1。养老金领取者(PensionerId(int Primary Key),姓名(字符串),UpdatedOn(日期时间),批准(位))


2。配偶(SpouseId(int Primary Key),PensionerId(外键)姓名(字符串))


3。 DeathEvent(EventId(int Primary Key),PensionerId(外键),Date(DateTime))


我想做的是每当用户在表Spouse和DeathEvent中添加或编辑数据时;因此,应通过将UpdatedOn设置为当前日期并将字段Approve设为false来修改Pensioner表中的UpdatedOn和Approve字段。


>>编辑:26/04/2017 22 :37


我观察到有些字段我已经通过ModelMetaData.cs文件调用自定义验证。


问题是当我们编辑记录中的任何字段并尝试保存时,会抛出异常:例如,为名称字段定义的自定义验证规则是限制重复条目变得麻烦,编辑记录时会发现
具有相同名称的记录。


那么,有没有办法管理自定义验证,检查其余记录中的重复条目,而不是正在记录的记录编辑。


>> 


请帮忙。


最诚挚的问候,


Arun

解决方案

嗨ArunKhatri,


>>我想要做的是,只要用户在表Spouse和DeathEvent中添加或编辑数据,就应该通过设置UpdatedOn来修改Pensioner表中的UpdatedOn和Approve字段当前日期和字段
批准为假。


您可以检索相关的养老金领取者并更新它,如下所示:


< pre class ="prettyprint"> var spouse = new Spouse(){SpouseId = 1,PensionerId = 1,Name =" Test" };
db.Spouses.Add(配偶);
var pension = db.Pensioners.Find(1);
pension.UpdatedOn = DateTime.Now;
pension.Approve = false;
db.SaveChanges();

>> 那么,有吗管理自定义验证的任何方式,检查其余记录中的重复条目,而不是正在编辑的记录。


根据你的描述,问题是更多相关的asp.net mvc,我建议你可以在asp.net MVC论坛上发布这个问题适当的支持。


https://forums.asp.net/1146。 aspx / 1?MVC


感谢您的理解。


致以最诚挚的问候,


Cole Wu


Sir,

I am developing an application in ASP.net MVC using Entity frameworks, I have the following tables:

1. Pensioner (PensionerId (int Primary Key), Name(string), UpdatedOn(DateTime), Approve(bit))

2. Spouse(SpouseId (int Primary Key), PensionerId(Foreign Key) Name(string))

3. DeathEvent (EventId (int Primary Key), PensionerId (Foreign Key), Date(DateTime))

What I want to do is whenever a user adds or edit data in tables Spouse and DeathEvent the fields UpdatedOn and Approve in Pensioner table should accordingly get modified by setting UpdatedOn with the current date and the field Approve as false.

>>EDIT: 26/04/2017 22:37

I observed that there are some fields on which I have put custom validation which I call through ModelMetaData.cs file.

The problem is that when we edit any field in the record and try to save, an exception is thrown: for example, a custom validation rule defined for say Name field is to restrict duplicate entry becomes troublesome while editing the record for it will find the record with the same name.

So, is there any way to manage custom validations which, checks for a duplicate entry in rest of the records and not the record that is being edited.

>> 

Please help.

Best Regards,

Arun

解决方案

Hi ArunKhatri,

>>What I want to do is whenever a user adds or edit data in tables Spouse and DeathEvent the fields UpdatedOn and Approve in Pensioner table should accordingly get modified by setting UpdatedOn with the current date and the field Approve as false.

You could retrieve related Pensioner and update it, like this:

 var spouse = new Spouse() { SpouseId = 1, PensionerId = 1,Name = "Test" };
db.Spouses.Add(spouse);
var pension = db.Pensioners.Find(1);
pension.UpdatedOn = DateTime.Now;
pension.Approve = false;
db.SaveChanges();

>>So, is there any way to manage custom validations which, checks for a duplicate entry in rest of the records and not the record that is being edited.

According to your description, the issue is more related asp.net mvc, I would suggest that you could post the issue on asp.net MVC forum for suitable support.

https://forums.asp.net/1146.aspx/1?MVC

Thanks for your understanding.

Best regards,

Cole Wu


这篇关于如何在另一个表中创建新记录时修改表中的字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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