NHibernate:在OnPreInsert中生成自定义主键 [英] NHibernate: Generate Custom Primary Key in OnPreInsert

查看:68
本文介绍了NHibernate:在OnPreInsert中生成自定义主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在OData服务中,我必须在OnPreInsert事件处理程序中创建一个自定义主键.

In my OData service I have to create a custom primary key in the OnPreInsert event handler.

我知道我不能使用@event.Id来分配密钥,因为它没有公开setter属性.

I know I can't use @event.Id to assign the key because it doesn't expose the setter property.

我使用反射来设置此属性的值,如下所示:

I used the reflection to set the value of this property as shown below:

public bool OnPreInsert(PreInsertEvent @event)
{

    if(@event.Entity is MyEnity)
    {            
        var myEntity = @event.Entity as MyEnity;


        string newKey = GetCustomKey(...);

            myEntity.myId = newKey;                
            var property = typeof(AbstractPreDatabaseOperationEvent).GetProperty("Id");
            if (property != null)
            {
                property.SetValue(@event,newKey);
            }
        }

        return false;
}

在调试模式下,我可以看到@event.Id的值已正确初始化,但是数据库中保存的密钥不是我在OnPreInsert事件处理程序中生成的密钥.

During the debug mode I can see that the value of @event.Id is initialized properly, however the key saved in the database is not the one I generated in the OnPreInsert event handler.

我在做什么错了?

推荐答案

我找不到某种方法来使用反射来实现我在上面的问题中描述的内容.我之所以尝试使用反射,是因为我不了解NHibernate中可用的Generators(因为我是NHibernate的新手).

I couldn't find some way to use the reflection to achieve what I described in my question above. I tried to use reflection because I didn't know about the Generators available in NHibernate (as I am new to NHibernate).

我有一个名为 sys_params 的表,其中包含不同表的下一个键值.我的目标是获取表 my_entity 的下一个键,将其分配给新记录的主键,增加 sys_params 表中的下一个键值,然后保存新记录到数据库中.

I have a table named sys_params which holds the next key values for different tables. My target was to fetch the next key for my table my_entity, assign it to the primary key of the new record, increment the next key value in the sys_params table and save the new record into the database.

为实现这一目标,我定义了以下类.

To achieve this first I defined following classes.

public class NextIdGenerator : TableGenerator
{
}

public class NextIdGeneratorDef : IGeneratorDef
{
    public string Class
    {
        get { return typeof(NextIdGenerator).AssemblyQualifiedName; }
    }

    public object Params
    {
        get { return null; }
    }

    public Type DefaultReturnType
    {
        get { return typeof(int); }
    }

    public bool SupportedAsCollectionElementId
    {
        get { return true; }
    }
}

然后在我的映射类中,我定义了生成器,如下所示:

And then in my mapping class I defined the generator like below:

public class MyEnityMap : ClassMapping<MyEnity>
{
    public MyEnityMap()
    {
        Table("my_entity");
        Id(p => p.myId,
           m=>{
               m.Column("my_id");
               m.Generator(new NextIdGeneratorDef(), g =>g.Params( new
               {
                   table = "sys_params",
                   column = "param_nextvalue",
                   where = "table_name = 'my_entity'"
               }));
           });

        .......                   
    }
}

希望这对其他人有帮助.对此解决方案的改进表示高度赞赏.

Hope this will help someone else. Improvements to this solution are highly appreciated.

这篇关于NHibernate:在OnPreInsert中生成自定义主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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