简体   繁体   English

使用流畅的nhibernate访问现有数据库表

[英]Accessing an existing database table using fluent nhibernate

I am a beginning at nhibernate and I am trying to connect to a pre-existing database table using fluent-nhibernate. 我是nhibernate的开始,我正在尝试使用fluent-nhibernate连接到预先存在的数据库表。 The table has no id field, and I am stuck as to how to get queries to work. 该表没有id字段,我不知道如何让查询工作。 I get an error - "Field ID does not exist in ". 我收到一个错误 - “字段ID不存在”。

The table is in a DB2 database, so some of tools for autogenerating code are not applicable. 该表位于DB2数据库中,因此一些用于自动生成代码的工具不适用。

Update 更新

The file and the class looks rather like the following (where where WHO is the primary key) 该文件和类看起来很像以下(WHO是主键的地方)

public class Nougal
{
    public virtual string WHO { get; set; }
    public virtual string YN1 { get; set; }
    public virtual string YN2 { get; set; }
    public virtual string YN3 { get; set; }
    public virtual string YN4 { get; set; }

You must have some sort of id in order to have NH properly mapping your table as an entity to fit it properly in the identity map of the session. 您必须拥有某种ID才能让NH正确地将您的表映射为实体,以便在会话的身份映射中正确地适应它。 Usually this is represented by the primary key in your table. 通常,这由表中的主键表示。 Even if not defined in the table try to identify what distinguish each record and map it as the table identifier. 即使未在表中定义,也要尝试识别区分每条记录的内容并将其映射为表标识符。 This could possibly be a composite-id. 这可能是复合ID。

I have it working, I had to create a map, and point the Id field at the column that is being used as primary key. 我有它工作,我不得不创建一个地图,并将Id字段指向用作主键的列。

public class FILENAMEMap : ClassMap<FILENAME>
{
    public FILENAMEMap()
    {     
        Table("FILENAME");
        Id(x => x.Id).Column("WHO");
        Map(x => x.YN1).Column("YN1");
        Map(x => x.YN2).Column("YN2");

I think that this has done it, so thanks for pointers, etc. 我认为这已经做到了,所以感谢指针等。

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

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