LinqConnect does not put [ ] around field names that match SQL key words
Posted: Fri 15 Nov 2019 16:22
I'm using SQL Server, and I have a class called Email. It looks something like this:
[Table(Name="Emails")
public class Email
{
public int EmailID {get;set;}
public string To {get; set;}
public string Subject {get; set;}
public string Body {get; set;}
}
Assume the fields are properly mapped to fields with the same names in the Emails table.
When I do an insert using this code:
var email = new Email { To="[email protected]", Subject="test", Body="test" };
var context = CreateContext();
context.Emails.InsertOnSubmit(email);
context.SubmitChanges();
I get an exception from SQL Server:
Devart.Data.Linq.LinqCommandExecutionException: Error on executing DbCommand. ---> System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'To'.
The issue is that LinqConnect is executing an INSERT statement, but failing to put [ ] around the field names to guard against field names that are potentially keywords for SQL Server.
This is something that works fine in Linq to SQL.
Is there a way around this issue other than renaming the fields?
[Table(Name="Emails")
public class Email
{
public int EmailID {get;set;}
public string To {get; set;}
public string Subject {get; set;}
public string Body {get; set;}
}
Assume the fields are properly mapped to fields with the same names in the Emails table.
When I do an insert using this code:
var email = new Email { To="[email protected]", Subject="test", Body="test" };
var context = CreateContext();
context.Emails.InsertOnSubmit(email);
context.SubmitChanges();
I get an exception from SQL Server:
Devart.Data.Linq.LinqCommandExecutionException: Error on executing DbCommand. ---> System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'To'.
The issue is that LinqConnect is executing an INSERT statement, but failing to put [ ] around the field names to guard against field names that are potentially keywords for SQL Server.
This is something that works fine in Linq to SQL.
Is there a way around this issue other than renaming the fields?