locked
UniqueIdentifier with LINQ to SQL and SQL Server 2000 RRS feed

  • Question

  • User1589142742 posted

    I'm using VS 2008 SP1, .NET Framework 3.5 sp1, SQL Express 2005.
    Dynamic Data consumes a DataContext provided by a proper LINQ to SQL class.
    It works great, so I deeply customized page and fields.
    Now I need to setup my 'product' on a SQL Server 2000 environment.

    When I switch the DataContext's connectionstring to SQL 2000...

    1    The primary key column of type 'UniqueIdentifier' cannot be generated by the server. 
    in
    System.Data.Linq.SqlClient.QueryConverter.GetIdentityExpression(MetaDataMember id, Boolean isOutputFromInsert)
    in
    System.Data.Linq.SqlClient.QueryConverter.VisitInsert(Expression item, LambdaExpression resultSelector)
    in
    System.Data.Linq.SqlClient.QueryConverter.VisitDataManipulationCall(MethodCallExpression mc)
    in System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc)
    in
    System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
    in
    System.Data.Linq.SqlClient.QueryConverter.ConvertOuter(Expression node)
    in
    System.Data.Linq.SqlClient.SqlProvider.BuildQuery(Expression query, SqlNodeAnnotations annotations)
    in System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
    in
    System.Data.Linq.ChangeDirector.StandardChangeDirector.DynamicInsert(TrackedObject item)
    in
    System.Data.Linq.ChangeDirector.StandardChangeDirector.Insert(TrackedObject item)
    in
    System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode)
    in
    System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)
    in System.Data.Linq.DataContext.SubmitChanges()
    in System.Web.UI.WebControls.LinqToSqlWrapper.SubmitChanges(DataContext dataContext)
    in
    System.Web.UI.WebControls.LinqDataSourceView.InsertDataObject(Object dataContext, Object table, Object newDataObject)
    in
    System.Web.UI.WebControls.LinqDataSourceView.ExecuteInsert(IDictionary values)
    in
    System.Web.UI.DataSourceView.Insert(IDictionary values, DataSourceViewOperationCallback callback)
    in System.Web.UI.WebControls.DetailsView.HandleInsert(String commandArg, Boolean causesValidation)
    in System.Web.UI.WebControls.DetailsView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup)
    in
    System.Web.UI.WebControls.DetailsView.OnBubbleEvent(Object source, EventArgs e)
    in
    System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
    in
    System.Web.UI.WebControls.DetailsViewRow.OnBubbleEvent(Object source, EventArgs e)
    in
    System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
    in System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs e)
    in System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
    in
    System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
    in
    System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
    in
    System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
    in
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

    This happens when inserting data (List and Details works properly).
    As first step, I tried this workaround, and it works very well on everyday's LINQ to SQL,
    but it seems that implicit usage by Dynamic Data, brings some unespected behaviours,
    I don't get an exception anymore, but my UniqueIdentifier is persisted Empty ('00000000-0000-0000-0000-000000000000').

    I'm digging into the code, however any help would be really appreciated.
    Andrea

    Tuesday, October 14, 2008 11:06 AM

Answers

All replies

  • User-330204900 posted

    Hi A.Azzola, have alook at this thread here:Using UniqueIdentifier with Dynamic Data.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 14, 2008 11:41 AM
  • User-1005219520 posted

    Make sure the SQL 2000 schema has the default set to NEWID()

    That's a pretty good error message, I'd work with it. Create a simple table with a uniqueidentifier and try to repro your problem.

    >>but my UniqueIdentifier is persisted Empty ('00000000-0000-0000-0000-000000000000').
    that indicated that the client is not initializing the GUID so the zero GUID gets sent. For a uniqueidentifier  you will only be able to insert this once in a table then you will get a duplicate error. The link Steve gave should work, although my preference is to have SQL generate it with NEWID()

    Tuesday, October 14, 2008 2:55 PM
  • User1589142742 posted

    The solution linked by Steve, works well, sorry... I need to improve my searching skills [:(].
    Rick, the default value is already NEWID(), but it still gets overriden with the Guid.Empty value provided by the run-time
    (Auto Generated Value is false)...maybe I'm missing something...

    This is my temporary solution:

    1. My Guid_Edit.ascx.cs
       
      using System;
      using System.Collections.Specialized;
      
      public partial class Guid_EditField : System.Web.DynamicData.FieldTemplateUserControl
      {
          protected override void ExtractValues(IOrderedDictionary dictionary)
          {
              dictionary[Column.Name] = Guid.NewGuid();
          }
      }
       
    2. My MyTable.cs
       
          [MetadataType(typeof(MyTableMetaData))]
      public partial class MyTable
      {
      <documentation><item_history> }

      public class MyTableMetaData
      {
      [UIHint("Guid")]
      public System.Guid ID { get; set; }
      }</item_history></documentation>

    It needs improvements but... works fine to me.
    Thanks everyone!

    Wednesday, October 15, 2008 5:14 AM