ORM - 使用C#保存/恢复数据库对象的默认值

时间:2022-09-25 08:58:32

In order to assist users with repetitve data entry, I am trying to implement a system where many of the previous properties are remembered when adding new data.

为了帮助用户重复输入数据,我正在尝试实现一个系统,在添加新数据时会记住许多以前的属性。

Is it possible to use the Properties.Settings.Default.MySetting functionality or is there a better method for doing this kind of thing?

是否可以使用Properties.Settings.Default.MySetting功能,还是有更好的方法来做这种事情?

2 个解决方案

#1


Couldn't you just make a (deep) copy of the previous object, and use that as the next object, allowing users to overwrite any fields that changed? This way, the fields would be what the user last entered, individualized per user, and updated as they chnaged those fields.

难道你不能只创建上一个对象的(深层)副本,并将其用作下一个对象,允许用户覆盖任何已更改的字段吗?这样,字段将是用户上次输入的内容,每个用户个性化,并在他们对这些字段进行更新时进行更新。

If you have a way of remembering the last thing entered per user, you cold even preserve this between sessions.

如果你有办法记住每个用户输入的最后一件事,你甚至会在会话之间保留这个。

The OP comments:

OP意见:

Unfortunately, making a deep copy of an object messes up the objectcontext when relationships are involved. A new object with relationships either needs to have new relational objects created or existing objects queried from the database.

不幸的是,当涉及关系时,制作对象的深层副本会弄乱对象上下文。具有关系的新对象需要创建新的关系对象或从数据库查询现有对象。

So? If the relation is to an another entity (a foreign key in the database), it's a uses-a relationship, and you just retain it. If it's an attribute, you copy it.

所以?如果关系是另一个实体(数据库中的外键),则它是一个uses-a关系,你只需保留它。如果它是属性,则复制它。

For example, lets say your form is data entry about employees, and ithas a drop down for, I dunno, employeeType, that's either "Exempt" (no overtime) or "Non-exempt" (gets overtime). You pulled the values for employeeType from the database, and you want the next employee entered to have the same values as the last entered employee, to save the data entry people keystrokes. So your deep copy would just associate the copied employee with the same database employeeType.

例如,假设您的表单是关于员工的数据输入,并且它有一个下拉,我不知道,employeeType,这是“免除”(没有加班)或“非免除”(获得加班)。您从数据库中提取了employeeType的值,并且您希望输入的下一个员工具有与上次输入的员工相同的值,以保存数据条目人员击键。因此,您的深层副本只会将复制的员工与相同的数据库employeeType相关联。

But for attribute data (like name), you'd make a copy.

但对于属性数据(如名称),您可以制作副本。

#2


It depends on what you're trying to achieve. The good thing about using the MySetting functionality is that the "Most Recent" properties can be persisted the next time the application is closed.

这取决于你想要达到的目标。使用MySetting功能的好处是,下次关闭应用程序时可以保留“最新”属性。

I'm assuming this is a winforms application so I'd possibly keep a cached instance of the last save of each of the backing objects in a hashtable somewhere and then when you create a new form, look up the backing object in the hashtable and bind the required properties to the new instance of the form.

我假设这是一个winforms应用程序,所以我可能会在某个哈希表中保留每个后备对象的最后一次保存的缓存实例,然后在创建新表单时,在哈希表中查找后备对象,将所需的属性绑定到窗体的新实例。

You can then serialize and persist the entire hashtable to the MySettings object if you like so it can be used each time the user accesses the application.

然后,如果您愿意,可以将整个哈希表序列化并保留到MySettings对象,以便每次用户访问应用程序时都可以使用它。

#1


Couldn't you just make a (deep) copy of the previous object, and use that as the next object, allowing users to overwrite any fields that changed? This way, the fields would be what the user last entered, individualized per user, and updated as they chnaged those fields.

难道你不能只创建上一个对象的(深层)副本,并将其用作下一个对象,允许用户覆盖任何已更改的字段吗?这样,字段将是用户上次输入的内容,每个用户个性化,并在他们对这些字段进行更新时进行更新。

If you have a way of remembering the last thing entered per user, you cold even preserve this between sessions.

如果你有办法记住每个用户输入的最后一件事,你甚至会在会话之间保留这个。

The OP comments:

OP意见:

Unfortunately, making a deep copy of an object messes up the objectcontext when relationships are involved. A new object with relationships either needs to have new relational objects created or existing objects queried from the database.

不幸的是,当涉及关系时,制作对象的深层副本会弄乱对象上下文。具有关系的新对象需要创建新的关系对象或从数据库查询现有对象。

So? If the relation is to an another entity (a foreign key in the database), it's a uses-a relationship, and you just retain it. If it's an attribute, you copy it.

所以?如果关系是另一个实体(数据库中的外键),则它是一个uses-a关系,你只需保留它。如果它是属性,则复制它。

For example, lets say your form is data entry about employees, and ithas a drop down for, I dunno, employeeType, that's either "Exempt" (no overtime) or "Non-exempt" (gets overtime). You pulled the values for employeeType from the database, and you want the next employee entered to have the same values as the last entered employee, to save the data entry people keystrokes. So your deep copy would just associate the copied employee with the same database employeeType.

例如,假设您的表单是关于员工的数据输入,并且它有一个下拉,我不知道,employeeType,这是“免除”(没有加班)或“非免除”(获得加班)。您从数据库中提取了employeeType的值,并且您希望输入的下一个员工具有与上次输入的员工相同的值,以保存数据条目人员击键。因此,您的深层副本只会将复制的员工与相同的数据库employeeType相关联。

But for attribute data (like name), you'd make a copy.

但对于属性数据(如名称),您可以制作副本。

#2


It depends on what you're trying to achieve. The good thing about using the MySetting functionality is that the "Most Recent" properties can be persisted the next time the application is closed.

这取决于你想要达到的目标。使用MySetting功能的好处是,下次关闭应用程序时可以保留“最新”属性。

I'm assuming this is a winforms application so I'd possibly keep a cached instance of the last save of each of the backing objects in a hashtable somewhere and then when you create a new form, look up the backing object in the hashtable and bind the required properties to the new instance of the form.

我假设这是一个winforms应用程序,所以我可能会在某个哈希表中保留每个后备对象的最后一次保存的缓存实例,然后在创建新表单时,在哈希表中查找后备对象,将所需的属性绑定到窗体的新实例。

You can then serialize and persist the entire hashtable to the MySettings object if you like so it can be used each time the user accesses the application.

然后,如果您愿意,可以将整个哈希表序列化并保留到MySettings对象,以便每次用户访问应用程序时都可以使用它。