简体   繁体   English

您能否在nhibernate / fluent nhibernate中的提交时动态填充db值?

[英]Can you dynamically fill in a db value on commit in nhibernate/fluent nhibernate?

I am using jquery full calendar and one of the things it has is an id that should be unique per appointment or the same per a bunch of appointments. 我正在使用jquery完整日历,它具有的功能之一是每个约会应该唯一的ID或每组约会都应该相同的ID。

If you have the bunch of the same id's then all those appointments are considered repeating appointments. 如果您有一堆相同的ID,那么所有这些约会都被视为重复约会。

I want to generate a repeating id for each of my appointment and apply it to all the appointments that should have that repeating id. 我想为我的每个约会生成一个重复ID,并将其应用于应该具有该重复ID的所有约会。

I figured that the best way to do this would be to use the primary key of my appointment table. 我认为最好的方法是使用约会表的主键。

Example

PK: 1, Title: Test1,  Repeating Id: 1
PK: 2, Title: Test2,  Repeating Id: 2
PK: 3, Title: Test2,  Repeating Id: 2
PK: 4, Title: Test2,  Repeating Id: 2
PK: 5, Title: Test3,  Repeating Id: 5

So the first and last row don't have any other rows that repeat with it. 因此,第一行和最后一行没有其他重复的行。

Rows 2 to 4 do repeat with each other. 第2至4行确实会重复。

Now in my code I generate all the repeating rows before insert. 现在在我的代码中,我在插入之前生成所有重复行。 Since nhibernate does batch inserts I do all my inserts at once. 由于nhibernate可以批量插入,因此我可以一次完成所有插入操作。 So at this time I do not know what the PK will be for any of the rows. 因此,目前我不知道任何行的PK。

I am wondering can I somehow tell it to take the first item that should be inserted and find it's PK out and apply it to all the following items repeatingId column? 我想知道我能否以某种方式告诉它取下应该插入的第一个项目并找到它的PK,然后将其应用于以下所有所有重复项目列?

Right now I can only think of one way to do that and that is insert the first item and then get the PK back and then batch insert all the other items after with the repeating id filled in. 现在,我只能想到一种方法,即插入第一个项目,然后取回PK,然后在填写重复的ID后,批量插入所有其他项目。

I'm not sure how you will save the first element of a repeating group... but NH batches saves, not id assignments, so you can just do: 我不确定如何保存重复组的第一个元素...但是会保存NH批处理,而不是ID分配,因此您可以执行以下操作:

var repeatingId = session.Save(firstAppointmentOfTheGroup)
foreach (var appointment in repeating)
{
    appointment.RepeatingId = repeatingId;
    session.Save(appointment);
}

That said, I don't understand why you are replicating the model that jquery uses in your model/DB. 就是说,我不明白为什么要复制jquery在模型/数据库中使用的模型。

Instead, you should use a normalized model (with RepeatingAppointment having a collection of Appointments) and project that when using it. 相反,您应该使用规范化的模型(其中RepeatingAppointment具有约会集合)并在使用时对其进行投影。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM