8

I am trying to create a mapping to a database table that has no primary keys/references.

public class TestMap : ClassMap<<Test>Test> {

    public TestMap() {

        WithTable("TestTable");

        Map(x => x.TestColumn);

    }

}

This fails and expects id or composite-id. Is this possible in fluent nhibernate?

Adnan
  • 25,882
  • 18
  • 81
  • 110
  • Is there an 'unmarked' primary key - i.e do some collection of fields make a primary key, but the table doesn't have a primary key defined? – Nick.Mc Apr 08 '13 at 11:57

6 Answers6

5

In Oracle at least, I have used "ROWID" for this. For mssql you might use the "ROW_NUMBER()" builtin function for readonly access to the table, but I haven't tried that...

Tompi
  • 218
  • 2
  • 10
  • What a glorious little hack. Just tried mapping an ID column to ROWID and NHibernate doesn't seem to have any issues. THANKS! – rie819 Dec 11 '13 at 15:18
  • It works... and I don't feel worse than I felt in the first place after having a table without an ID. – War Gravy Apr 24 '20 at 16:21
2

No. You'll have to add a surrogate primary key, such as an identity column in SQL Server, to map this table. As far as I know, this isn't supported by NHibernate itself.

Why don't you have a primary key on this table?

Jamie Ide
  • 48,427
  • 16
  • 81
  • 117
  • 5
    I'd also like an answer to this question. Sometimes (actually most of the time) we're not working in greenfield scenarios. We are instead laying a new fluent NHibernate DAL over an existing db schema that can't be modified. I have a table without an identity column and well-definied keys. Believe me, if I could change this schema, we would, but we have a requirement not to change the database schema. – Peter Walke Mar 16 '11 at 12:59
0

If we can bring a column from table having no primary key/identity coulmn, then we can use fluent as below:

Id(x => x.TempID).Column("TempID");
Ivan Ferić
  • 4,725
  • 11
  • 37
  • 47
Jiss
  • 1
0

If the table contains data that belongs to another entity, you could map it as a collection of components. Components are not identified by themselves, but they belong to another entity, which is identified.

Stefan Steinegger
  • 63,782
  • 15
  • 129
  • 193
0

You can map an entity to a table without keys defined in the database. I do so in legacy SQL Server databases. However, the table must have a candidate key (some set of columns that actually stores a unique combination of values). The concept of entity involves the notion of some kind of identity. Instead of this, what you're trying in your code is to map an entity without identity, wich isn't possible.

Apocatastasis
  • 500
  • 1
  • 9
  • 21
0

This functionality isn't supported by nhibernate as far as I know. As a general rule of thumb, however, you should really always have some kind of ID and if you find yourself in a situation where you think you don't need one you should assess your data model. An ID, whether it be a table-specific primary key, or a surrogate key from another table, should exist. This not only ensures that nhibernate can process the table, but helps performance via indexing.

Before you start assuming nhibernate isn't going to fulfill your needs, consider why you don't have a key on the table and what kind of sense it makes not to have one.

Fourth
  • 9,163
  • 1
  • 23
  • 28