This is a migrated thread and some comments may be shown as answers.

[Solved] LinqDataSource + SQL View Calculated Column = Update Fail

1 Answer 230 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Wyatt
Top achievements
Rank 1
Wyatt asked on 04 Nov 2009, 03:54 AM

Using this example, I was able to create a working CRUD interface using a SQL view; however, when I added calculated columns to the view, I received the following error.

Sys.WebForms.PageRequestManagerServerErrorException: Update or insert of view or function 'dbo.vwEnum' failed because it contains a derived or constant field.

Here's the table class definition.  CreateUser and UpdateUser are the calculated columns, and it works if they are removed.  Does anyone know how to prevent the calculated columns from being updated?

  <Table Name="dbo.vwEnum" Member="vwEnums">  
    <Type Name="vwEnum">  
      <Column Name="EnumId" Type="System.Int32" DbType="Int NOT NULL IDENTITY" IsPrimaryKey="true" IsDbGenerated="true" CanBeNull="false" /> 
      <Column Name="Created" Type="System.DateTime" DbType="DateTime NOT NULL" CanBeNull="false" /> 
      <Column Name="CreateUserId" Type="System.Int32" DbType="Int NOT NULL" CanBeNull="false" /> 
      <Column Name="Updated" Type="System.DateTime" DbType="DateTime NOT NULL" CanBeNull="false" /> 
      <Column Name="UpdateUserId" Type="System.Int32" DbType="Int NOT NULL" CanBeNull="false" /> 
      <Column Name="Name" Type="System.String" DbType="VarChar(50) NOT NULL" CanBeNull="false" /> 
      <Column Name="Code" Type="System.String" DbType="VarChar(20) NOT NULL" CanBeNull="false" /> 
      <Column Name="Description" Type="System.String" DbType="VarChar(1000) NOT NULL" CanBeNull="false" /> 
      <Column Name="Attributes" Type="System.Xml.Linq.XElement" DbType="Xml" CanBeNull="true" UpdateCheck="Never" /> 
      <Column Name="CreateUser" Type="System.String" DbType="VarChar(153)" IsReadOnly="true" CanBeNull="true" /> 
      <Column Name="UpdateUser" Type="System.String" DbType="VarChar(153)" IsReadOnly="true" CanBeNull="true" /> 
      <Association Name="vwEnum_vwEnumValue" Member="vwEnumValues" ThisKey="EnumId" OtherKey="EnumId" Type="vwEnumValue" /> 
    </Type> 
  </Table> 

1 Answer, 1 is accepted

Sort by
0
Wyatt
Top achievements
Rank 1
answered on 04 Nov 2009, 08:11 PM
Fixed!  Set the Auto Generated Value to true.

For some reason setting the column's ReadOnly property to true in the Visual Studio MSLinqToSQLGenerator doesn't prevent the column from being inserted or updated.  Here, the solution is described under the subheader titled "Avoiding Explicit Setting of Database-Generated Values on Insert or Update".

"LINQ to SQL handles this situation automatically for identity (auto-increment) and rowguidcol (database-generated GUID) and timestamp columns. In other cases, you should manually set IsDbGenerated=true and AutoSync=Always/OnInsert/OnUpdate properties."
Tags
Grid
Asked by
Wyatt
Top achievements
Rank 1
Answers by
Wyatt
Top achievements
Rank 1
Share this question
or