I have a table in SQL Server 2008 created as follows:
EDIT: I had been doing some tests setting ANSI_NULLS OFF and ON and i mistakenly pasted the create table statement with ANSI_NULLS OFF, i have tried both ways and i still get the error but wanted to fix this. Hopefully there is no other paste error.
EDIT 2: I forgot to point out that if i query the original table (TblValues) everything works, the error about the ANSI_NULLS option only occurs when using the db view (TblValuesView).
SET NUMERIC_ROUNDABORT OFF;
SET ANSI_PADDING, ANSI_WARNINGS, CONCAT_NULL_YIELDS_NULL, ARITHABORT,
QUOTED_IDENTIFIER, ANSI_NULLS ON;
GO
CREATE TABLE TblValues
(
Context nvarchar(8) NOT NULL,
ParentID uniqueidentifier NOT NULL,
RowSeq int NOT NULL,
FieldValues xml NOT NULL,
FieldProperties xml NOT NULL,
) ON [PRIMARY]
TEXTIMAGE_ON [PRIMARY]
I then loaded this table to my data model and am able to query it using LINQ to EF. The XML content in the FieldValues column is intended to be like this:
<FieldValues>
<FieldValue fieldName="txtName">
<DataType>String</DataType>
<FldValue>field value</FldValue>
</FieldValue>
...
</FieldValues>
Since i need to provide the values in the xml columns in an easier way to read i created a view for this table as follows:
SET NUMERIC_ROUNDABORT OFF;
SET ANSI_PADDING, ANSI_WARNINGS, CONCAT_NULL_YIELDS_NULL, ARITHABORT,
QUOTED_IDENTIFIER, ANSI_NULLS ON;
GO
CREATE VIEW TblValuesView
WITH SCHEMABINDING
AS
SELECT TblValuesView.Context,
TblValuesView.ParentID,
TblValuesView.RowSeq,
FldValue.value('(@fieldName)[1]', 'nvarchar(30)') AS FieldName,
FldValue.value('(DataType)[1]', 'nvarchar(13)') AS DataType,
FldValue.value('(FldValue)[1]', 'nvarchar(max)') AS FldValue,
FROM TblValues CROSS APPLY FieldValues.nodes('/FieldValues/FieldValue') AS FldValues(FldValue)
GO
Then i added this view to the data model as well. Now if i query this view from SQL Server Management Studio i get the proper values as:
Context | ParentID | RowSeq | FieldName | DataType | FldValue
----------------------------------------------------------------
Ctx <id> 1 txtName String Field Value
But if perform the following LINQ query:
var FldValueViewQuery = (from row in CTX.TblValuesView select row);
foreach(TblValuesView tblValue in FldValueViewQuery)
System.Diagnostics.Trace.WriteLine(tblValue.FieldName);
I then get the following exception:
System.Data.EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details.
System.Data.SqlClient.SqlException: SELECT failed because the following SET options have incorrect settings: 'ANSI_NULLS'.
Verify that SET options are correct for use with indexed views and/or indexes on computed columns and/or filtered indexes and/or query notifications and/or XML data type methods and/or spatial index operations.
I can only get this error from SQL Server Mgmt studio if i do this:
SET ANSI_NULLS OFF
select * from TblValuesView
I have searched on whether if EF changes this option at runtime and i have already checked that my database has ANSI_NULLS enabled and defaulted to true. What other thing can i check? there are not many resources out there that deal with this scenario.
Thanks in advance!