views:

159

answers:

3

I am getting a cannot load entity error because of {"Incorrect syntax near the keyword 'File'."}

Here is my mapping file. I'm assuming I am not escaping something properly but I don't see what. I don't think it's my PreApplication.File table name. Thanks in advance!!

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Model" namespace="Model.PreApplication">
  <class name="File" table="PreApplication.File">

    <id name="ID" column="ID" type="Int32" unsaved-value="0">
      <generator class="native" />
    </id>

    <property name="DateUploaded" column="DateUploaded" type="DateTime" not-null="true" />
    <property name="FileName" column="`FileName`" type="String" not-null="true" />
    <property name="OriginalFileName" column="OriginalFileName" type="String" not-null="true" />
    <property name="ContentType" column="ContentType" type="String" not-null="true" />

  </class>
</hibernate-mapping>

Edit: Using the suggestions provided, I've discovered that they problem is being caused by my table being called File. If I change the table attribute to "PreApplication.[File]" it works correctly, but I don't think that's the "NHibernate" way of doing it. I think I'm supposed to use the tick character (`) but I can't seem to make that work.

+2  A: 

Can you use a profiler which intercepts the SQL statements that are sent to the DB, or set the show_sql configuration property of NHibernate set to true, so that we can see the actual SQL statement that is executed ?

Frederik Gheysels
Thanks for the suggestions! I got tunnel vision and didn't think of this. Duh!
Mike C.
+3  A: 

I've found the correct way of fixing this. I found the schema attribute and have changed it as follows:

<class name="File" schema="PreApplication" table="`File`">

This properly escapes the File table name without having to hardcode []. Thanks for the help everyone!

Mike C.
A: 

"File" is a reserved keyword in T-SQL (see http://msdn.microsoft.com/en-us/library/ms189822.aspx). You should not use it as a table name. You can test this in a query tool for SQL Server:

select * from File

...and you'll get the same error:

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

You have to change the name of your table or use "[File]" syntax if you can't change the name.

Leo