流畅的NHibernate获取视图,无需唯一标识符 [英] Fluent NHibernate fetching view without unique identifier

查看:91
本文介绍了流畅的NHibernate获取视图,无需唯一标识符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图映射不带标识符的视图,但是nhibernate仍然会生成一个带有id列的sql(给我一个sql错误,因为db中不存在ID列).也许我误会了Id()构造函数?

I'm trying to map a view without an identifier, but nhibernate still generates a sql with the id column (giving me a sql error, since the ID column does not exists in the db). Maybe I'm misunderstanding the Id() constructor?

构造者评论:

创建一个ID,该ID在域对象中没有相应的属性, 或数据库中的一列.这主要用于只读访问 和/或视图.默认为带有增量"生成器的int身份.

Create an Id that doesn't have a corresponding property in the domain object, or a column in the database. This is mainly for use with read-only access and/or views. Defaults to an int identity with an "increment" generator.

public class PersonMapping : ClassMap<Person>
{
    public PersonMapping()
    {
        Table("person");
        ReadOnly();

        Id();
        Map(f => f.Name, "name");
    }
}

推荐答案

NHibernate需要一个ID.方法doc表示会创建一个ID,该ID在您的域对象中没有相应的属性-但是数据库仍然具有ID.

NHibernate requires an ID. The method doc says it creates an ID which has no corresponding property in your domain object - however the database still has an ID.

如果表中没有字段标记为标识符(必须是唯一的..),则可以尝试识别一些可以组合为复合ID的列.

If you have no field in your table to mark as an identifier (has to be unique..) maybe you can try to identify some columns which can be composed as an composite id.

例如,给出一个简单的链接表,该链接表将一些int链接到另一个int之类的

Given for example a simple link table which links some int to an other int like

A | B
-----
1 | 2
1 | 3
2 | 2

只要所有A/B组合都是唯一的,就可以使用Composite ID.

you could use Composite ID as long as all A/B combinations are unique.

public PersonMapping()
{
    [...]
     CompositeId()
         .KeyProperty(x => x.A)
         .KeyProperty(x => x.B);
    [...]
}

这篇关于流畅的NHibernate获取视图,无需唯一标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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