简体   繁体   English

使用Nhibernate映射没有主键的单行表

[英]Mapping a single row table without primary key using Nhibernate

I'm working with a legacy table which I cannot change . 我正在使用无法更改的旧表。 The database have a settings table which consists of a lot of columns (one for each setting) and only one row: 数据库具有一个设置表,该表由许多列(每个设置一个)和仅一行组成:

Columns: Setting1  | Setting2     | Setting3  | etc...
         ----------+--------------+-----------+-------------
Row1:    SomeValue | AnotherValue | LastValue | etc...

Now, this would be all fine if it wasn't for the fact that the table lacks a primary key. 现在,如果不是因为该表缺少主键,这一切都很好。 Obviously, the original developer didn't think it was necessary, as there's only one single row. 显然,原始开发人员认为没有必要,因为只有一行。

Is there any way of mapping this with Nhibernate? 有没有办法用Nhibernate映射它? I've already implemented a SQL based solution, but I'd love to have the flexibility and simplicity gained with Nhibernate. 我已经实现了基于SQL的解决方案,但是我很想拥有Nhibernate带来的灵活性和简单性。

I'm fearing the worst but, any ideas? 我担心最坏的想法,但是有什么想法吗?

Well, just pick any setting (column) as the Id. 好吧,只需选择任何设置(列)作为ID。 The only inconvenience is that if you need to change the setting that is the PK you'll have to delete and re-insert the row... 唯一的不便是,如果您需要更改PK设置,则必须删除并重新插入该行...

You could map a view that adds a dummy PK column: 您可以映射添加虚拟PK列的视图:

select 1 as SettingsId, Setting1, etc....

or load the object from a stored procedure. 或从存储过程中加载对象。

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

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