DB Column named “Order” with Fluent NHibernate

2019-06-26 02:10发布

I discovered that one of the tables of a legacy db I'm working on has a colum named "Order". Unfortunately I cannot change the DB structure.
My Fluent NHibernate class looks like

 public class SiteMap : AutoMap<Site>
{
    public SiteMap() {
        WithTable("Sites");
        Id(x => x.ID, "Id")
            .WithUnsavedValue(0)
            .GeneratedBy.Identity();
        Map(x => x.Name, "Name");
        //various columns mapping and then...
        Map(x => x.SiteOrder, "Order");
    }
}

I do not know if the problems is FluentNH or NHibernate itself but I can confirm that the problem lies in the "Order" reserved name.

How to solve this?

Update: as suggested putting in the form [Order] worked. Thanks!
But now I'm linked only to SQL2005?

2条回答
兄弟一词,经得起流年.
2楼-- · 2019-06-26 03:05

What's the database behind the scenes?

With MS SQL, you should be able to fix this by surrounding the "Order" field with square brackets

Map(x => x.SiteOrder, "[Order]");

Edit: square brackets should work in all version of MS SQL, as well as MS Access. Not sure about other platforms, but its likely to work in some others, too.

查看更多
Explosion°爆炸
3楼-- · 2019-06-26 03:12

Try putting Order in back ticks: ``Order. Since FluentNH is generating HBM files at runtime I imagine that should fix the problem.

查看更多
登录 后发表回答