English 中文(简体)
NHibernate - 姓名 主要制约因素?
原标题:NHibernate - Name Primary key constraint?

我们开始在我的工作场所使用NHibernate,包括绘制地图。 我们的德国律师协会想要的是初级和外国关键关系的统一名称。 我早就能够确定FK制约因素的名称,但查看了<代码><id>的文件;它不像现在这样看一看主要制约因素。 http://www.nhforge.org/doc/nh/en/index.html#mapping-declaration-id

我假定我有点失踪,因为这似乎只是一件基本的事情。

最佳回答

不幸的是,它没有得到支持。 这里有ugly workaround

问题回答

我在编印计划之后,用以下文字确定主要名称。 http://www.un.org/Depts/DGACM/index_french.htm

BEGIN TRANSACTION
DECLARE @Rename nvarchar(MAX)
DECLARE RenameCursor CURSOR FOR
    SELECT
             EXEC sp_rename   [  + c.CONSTRAINT_SCHEMA +  ].[  + c.CONSTRAINT_NAME +  ]  ,   PK_  + c.TABLE_NAME +    ,   OBJECT   
        FROM $(targetDb).INFORMATION_SCHEMA.TABLE_CONSTRAINTS c
        WHERE
            c.CONSTRAINT_TYPE =  PRIMARY KEY 
            AND
            c.TABLE_NAME IS NOT NULL
        ORDER BY c.TABLE_NAME
OPEN RenameCursor
FETCH NEXT
    FROM RenameCursor
    INTO @Rename
WHILE @@FETCH_STATUS = 0
BEGIN
    EXEC sp_executesql @Rename
    FETCH NEXT
        FROM RenameCursor
        INTO @Rename
END
CLOSE RenameCursor
DEALLOCATE RenameCursor
COMMIT TRANSACTION

http://www.primordialcode.com/blog/post/nhibernate-give-first-key-schemaexport-sql-server-sql-express”rel=“nofollow noreferer” http://www.primordialcode.com/blog/post/nhibernate-give-first-key-schemaexport-sql-server-sql-express 这里的工作是:

NHibernate doesn’t offer (yet) a facility to give a name to your primary key (nothing that I found however, I admit I’m not an NHibernate guru, but an average user). You can use an approach similar to that exposed in my previous post.

In this example I’m using SQL Server/SQL Express as my database engine and the queries are built with that in mind.

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false">
    <class name="SID.Sphera.Controls.Extended.ImageRegion.Entities.ImageSheetData, SID.Sphera.Controls.Extended"
    table="ImageRegionImageSheetData" lazy="false">
        <id name="_Id" access="field" column="IRISD_Id" type="guid">
            <generator class="guid" />
        </id>
        <property name="_Name" access="field" column="IRISD_Name" type="string" not-null="true" />
        <property name="_ResourceId" access="field" column="IRISD_ResourceId" type="guid" not-null="true" />
        <property name="_Width" access="field" column="IRISD_Width" not-null="true" type="int" />
        <property name="_Height" access="field" column="IRISD_Height" not-null="true" type="int" />
        <property name="_BackgroundImageId" access="field" column="IRISD_BackgroundImageId" type="guid"
         not-null="false" />
        <bag name="_sensitiveRegions" access="field" cascade="all-delete-orphan" lazy="false">
            <key column="IRIRD_ParentImageSheetId" foreign-key="FK_IRIRD_IRISD" />
            <one-to-many class="SID.Sphera.Controls.Extended.ImageRegion.Entities.ImageRegionData, SID.Sphera.Controls.Extended" />
        </bag>
    </class>
    <!-- Primary Key Rename -->
    <database-object>
        <create>
         DECLARE @pkName Varchar(255)
         ;
         SET @pkName= (
             SELECT [name] FROM sysobjects
             WHERE [xtype] =  PK 
             AND [parent_obj] = OBJECT_ID(N [dbo].[ImageRegionImageSheetData] )
         )
         ;
         Exec sp_rename @pkName,  PK_ImageRegionImageSheetData ,  OBJECT 
        </create>
        <drop/>
    </database-object>
</hibernate-mapping>

With this query you get the actual name of the primary key which was generated by NHibernate, this is specific to SQL server/SQL express and is you use a different database engine you have to adapt those queries (I know you loose the decoupling to the database engine offered by NHibernate, but you can setup some strategies to load different mappings according to you current dialect).

We use a system stored procedure that allow us to rename the object we got before.





相关问题
nHibernate one-to-many inserts but doesnt update

Instead of getting into code, I have a simple question. Default behavior for a simple one-to-many is that it inserts the child record then updates the foreign key column with the parent key. Has ...

How Do I copy an existing nhibernate object as a new object?

I a persisted NHibernate object that I would like to repersist as a new entity. How do I get NHibernate to save this object as if it was a new? I am thinking I might create a session interceptor to ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

NHibernate Search in a List using ICriteria

I have my class X : public class ClassX { public virtual IList<ClassY> ListY { get; set; } ... } My ClassX mapping (using Fluent) ... HasMany<ClassX>(x => x.ListY ) ....

热门标签