0

I use fluent nHibernate to connect to a SQL Server database. So this is my domain class:

public class Option
{
    public virtual long Id { get; set; }

    public virtual string User { get; set; }

    public virtual string SvnTrunkFolder { get; set; }

}

And this is my mapping class

public class OptionMap : ClassMap<Option>
{
    public OptionMap()
    { 
        Id(x => x.Id);
        Map(x => x.User);
        Map(x => x.SvnTrunkFolder);
    }
}

This domain class has a problem. 'User' is a keyword of SQL Server. So the create-statement nHibernate uses (as found in Visual Studios Debug Output) is:

create table mydb.[Option] (
    Id BIGINT IDENTITY NOT NULL,
   User NVARCHAR(255) null,
   SvnTrunkFolder NVARCHAR(255) null,
   primary key (Id)
)

When I paste this to my SQL Server Manager and try it I get the following error:

Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'User'.

My problem is, that nHibernate dont tell me about this. I get no exception, no error. Everything seems normal. But the table is not created. When I change the Property "User" to "WindowsUser" the table is created perfectly.

This is the function I use to connect to the database:

public ISessionFactory CreateSessionFactory(IDatabaseConnectionInformation connectInfo, Action<MappingConfiguration> mappings)
{
    var configure = Fluently.Configure();
    var dbConfig = MsSqlConfiguration.MsSql2012.ConnectionString(connectInfo.ConnectionString);
    var dbConfigWithSchema = dbConfig.DefaultSchema(connectInfo.Database);
    var fluentDb = configure.Database(dbConfigWithSchema);
    var fluentMap = fluentDb.Mappings(mappings);
    fluentMap = fluentMap.ExposeConfiguration(BuildSchema);
    return fluentMap.BuildSessionFactory();
}
private static void BuildSchema(Configuration config)
{
    new SchemaUpdate(config)
          .Execute(true, true);
}   

So my question is: Why did nHibernate did not tell me about the failure to create the table? Do I have to activate something? Or is this just a bug with this particular error?

Edit: This does not answer my question because my question is !not! how I can create this column. I can just rename it. My question is: why nHibernate does not throw an exception when its not able to create the table?

Gener4tor
  • 414
  • 3
  • 12
  • 40
  • No, it does not. My question is not how I can create this column. I can just rename it. My question is, why nHibernate does not throw an exception when its not able to create the table. – Gener4tor Dec 07 '22 at 12:38
  • *why nHibernate does not throw an exception* maybe it does ... but you never catch it, because you are doing [something wrong with the tasks (like not await)](https://dotnetfiddle.net/ZYFmaA) – Selvin Dec 07 '22 at 12:49
  • I dont see doing something with tasks. This is the Execute-function I use: "public void Execute(bool useStdOut, bool doUpdate);". There is also no sign of any thrown exception in the debug output + the exception-settings "break when Thrown" are on for the relevant exceptions. – Gener4tor Dec 07 '22 at 15:26

0 Answers0