ASP.NET Dynamic Data + guid

Last post 10-23-2009 6:33 AM by bignbullish. 25 replies.

Sort Posts:

  • ASP.NET Dynamic Data + guid

    12-11-2007, 2:50 PM
    • Member
      5 point Member
    • Ig0r
    • Member since 03-20-2006, 9:21 AM
    • Posts 5

    Hi,
    I use guid as my primary key.
    Insert command don't create new guid when i insert record.

    Is it possible to customize guid data filelds??

    Thanks

     Igor

  • Re: ASP.NET Dynamic Data + guid

    12-11-2007, 7:38 PM
    • Contributor
      2,024 point Contributor
    • marcind
    • Member since 09-06-2007, 5:11 AM
    • Redmond, WA
    • Posts 410
    • AspNetTeam

    Hi Igor,

    Dynamic Data treats Primary Keys in a special way (for example, hiding them from the autogenerated pages). Having an autogenerated GUID actually falls within the realm of SQL Server. You can achieve this by specifying that your GUID column should have a default value and that default value should be ther result of evaluating the newid() function (look at Example B here for more info: http://msdn2.microsoft.com/en-us/library/ms190348.aspx).

    Marcin Dobosz
    SDE, ASP.NET Team, Microsoft
    Read my blog
  • Re: ASP.NET Dynamic Data + guid

    12-12-2007, 4:42 AM
    • Member
      5 point Member
    • Ig0r
    • Member since 03-20-2006, 9:21 AM
    • Posts 5

    Hi Marcin,

    Thanks for the quick answer.
    Best Regards

    Igor

  • Re: ASP.NET Dynamic Data + guid

    12-12-2007, 7:06 AM
    • Member
      5 point Member
    • Ig0r
    • Member since 03-20-2006, 9:21 AM
    • Posts 5

    I still have the same problem after adding newid() function.
    I seems that linq to sql always insert 00000000-0000-0000-0000-000000000000 value in db.

    Any ideas?

    Thanks

  • Re: ASP.NET Dynamic Data + guid

    12-12-2007, 7:31 AM
    • Member
      5 point Member
    • Ig0r
    • Member since 03-20-2006, 9:21 AM
    • Posts 5

    One solution is to add newid() function in db and than change column property isDbGenerated:=True in linq to sql generated classes

    <Column(Storage:="_CustomerFaxTypeID", DbType:="UniqueIdentifier NOT NULL", IsPrimaryKey:=True, IsDbGenerated:=True)> _

    Problem is that i must change that property every time when i generate linq to sql classes.

    Is there any other solution??

    Igor

  • Re: ASP.NET Dynamic Data + guid

    12-12-2007, 1:04 PM
    Answer
    • Contributor
      2,024 point Contributor
    • marcind
    • Member since 09-06-2007, 5:11 AM
    • Redmond, WA
    • Posts 410
    • AspNetTeam

    Sorry, I forgot to mention that detail.

    You can set that property using the Linq to SQL designer and then it will automatically be included in the autogenerated code (unless you completely remove a table from your model and drag it in again - but this is an issue with Linq to SQL and not Dynamic Data):

    1. Open your Linq to SQL model
    2. Right-click on your GUID property and select Properties
    3. In the Properties Editor in the Data Section change the value of the "Auto Generated Value" property to true.
    Marcin Dobosz
    SDE, ASP.NET Team, Microsoft
    Read my blog
  • Re: ASP.NET Dynamic Data + guid

    04-14-2008, 5:43 PM
    • Member
      4 point Member
    • TonyHawe
    • Member since 04-14-2008, 9:22 PM
    • Posts 4

    I have a similar problem using an auto-incrementing numeric as a primary key.

    When adding a new item using the default template it creates a textbox for the ID.

    Hiding the column is no good as it will complain that the column is required.

    Whatever number you type in it will save the next available ID anyway, unless you type an existing ID, in which case it will complain the ID already exists.

     IsDbGenerated is set to true.

    Is there an easy, or even a dirty way around this?

    Hide the ID column and pass the max possible ID every time somehow?

  • Re: ASP.NET Dynamic Data + guid

    04-14-2008, 7:00 PM
    • Participant
      1,320 point Participant
    • scothu
    • Member since 12-10-2007, 6:54 AM
    • Redmond, WA
    • Posts 272
    • AspNetTeam
      Moderator

    First off I'm assuming that the field in your database is set as an identity column? If it is an identity column and a primary key then we will hide the field and it will automatically get incremented as each new row is put in. If you modified the database after you created your Linq Classes you probably should delete the table from the Linq designer and drag it back onto the designer from the database. On my machine a working table with an identity primary key looks like this:

     Auto Generated Value: true

    Auto-Sync: OnIsert

    Primary Key: true

     Note if we do not show a column and you want to add it you can always for the column to show by adding it manually:

    <asp:DynamicField DataField="fieldname" />

     ...Scott

    Scott Hunter
    PM, ASP.NET Team, Microsoft
  • Re: ASP.NET Dynamic Data + guid

    04-14-2008, 8:04 PM
    • Member
      4 point Member
    • TonyHawe
    • Member since 04-14-2008, 9:22 PM
    • Posts 4

    Sorry Scott, only feeling my way around these controls at the moment.

    Found the Fields collection in the DynamicDetailsView so I can omit the offending column when new is clicked cheers.

     If the boss decides they want the ID field displayed too I'll use renderhints to point to a custom _Edit Dynamic Data  field that just displays the value in a literal and shows "New" if the value is blank.

    Cheers for the speedy response.  

  • Re: ASP.NET Dynamic Data + guid

    04-14-2008, 8:24 PM
    • Contributor
      5,655 point Contributor
    • davidebb
    • Member since 06-11-2002, 12:31 PM
    • Redmond, WA
    • Posts 1,123
    • AspNetTeam

    Note that if you have a DynamicDetailsView, you are likely using the old December build, as this control no longer exists.  If possible, please give a try to the new build.

    thanks,
    David

  • Re: ASP.NET Dynamic Data + guid

    05-15-2008, 9:06 AM
    • Member
      27 point Member
    • aspnerd
    • Member since 05-14-2008, 6:03 PM
    • Posts 13

    I'm having a similar problem in SP1 beta and 3.5 sp1 beta...I don't see the options of autogenerate and I'm using an Entity Data Model

    Does anyone know how to let the dynamic data rely on the SQL generated GUID for the primary key field?

  • Re: ASP.NET Dynamic Data + guid

    05-15-2008, 9:30 AM
    • Member
      27 point Member
    • aspnerd
    • Member since 05-14-2008, 6:03 PM
    • Posts 13

    were you ever able to get this fixed? I'm having the same issue with Entity Data Model, but I don't see the options they are referring to.

  • Re: ASP.NET Dynamic Data + guid

    05-15-2008, 12:24 PM
    • Participant
      1,320 point Participant
    • scothu
    • Member since 12-10-2007, 6:54 AM
    • Redmond, WA
    • Posts 272
    • AspNetTeam
      Moderator

    Entity Framework does not have the ability to tell Dynamic Data whether a column is generated, so by default we always assume it's not.  To work around this, you can put a ScaffoldColumn(false) attribute on the property, e.g.

    [MetadataType(typeof(Products_MD))]   

    partial class Products {    }   

    partial class Products_MD {       

       [ScaffoldColumn(false)]       

       public object ProductID { get; set; }   

    }
     

     

    Scott Hunter
    PM, ASP.NET Team, Microsoft
  • Re: ASP.NET Dynamic Data + guid

    06-02-2008, 4:47 PM
    • Member
      27 point Member
    • aspnerd
    • Member since 05-14-2008, 6:03 PM
    • Posts 13

    So here is the working solution for this issue.

     Using the same namespace as your data model create a class.

     Then create your partial class of the table you wish to use a SQL server set newid() guid.

    Here is a copy of the class I created.

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.ComponentModel.DataAnnotations;
     

    namespace YourOwnDataModel
    {
         [MetadataType(typeof(ClientInformation_MD))]
         partial class ClientInformation
         {
     
              public ClientInformation()
              {
                   ClientGUID =
    Guid.NewGuid();
              }

         }

         partial class ClientInformation_MD
         {
              [
    Scaffold(false)]
              public object ClientGUID { get; set; }
         }

    }

  • Re: ASP.NET Dynamic Data + guid

    07-29-2008, 11:21 AM
    • Member
      36 point Member
    • zwitterion
    • Member since 08-25-2006, 7:08 PM
    • Posts 46

    I've been using aspnerd's solution since all my entities have autogenerated (numberic identity) keys and I don't want any of them in my forms. However it seems really wrong to have to break out a partial class every time we want to skip the scaffolding of an autogenerated key which should basically never be scaffolded anyway doesn't it? Is there any better way of doing this?

Page 1 of 2 (26 items) 1 2 Next >