Change generated attributes on fields

4 posts, 0 answers
  1. Tiago
    Tiago avatar
    4 posts
    Member since:
    Nov 2012

    Posted 03 Oct 2013 Link to this post

    Hi,
    I have a table with columns "ID", "uniqueID", etc etc ...
    The generated class, is assuming the "uniqueID" columns has my primary key, causing errors when I'm trying to insert a new record.

    private int _iD;
    [Column("ID", OpenAccessType = OpenAccessType.Int32, Length = 0, Scale = 0, SqlType = "int")]
    [Storage("_iD")]
    public virtual int ID
    {
        get
        {
            return this._iD;
        }
        set
        {
            this._iD = value;
        }
    }
             
    private Guid _uniqueID;
    [Column("UniqueID", OpenAccessType = OpenAccessType.Guid, IsBackendCalculated = true, IsPrimaryKey = true, Length = 0, Scale = 0, SqlType = "uniqueidentifier")]
    [Storage("_uniqueID")]
    public virtual Guid UniqueID
    {
        get
        {
            return this._uniqueID;
        }
        set
        {
            this._uniqueID = value;
        }
    }

    I've tried to create a new partial class to use the MetadataType attribute and override the Column attribute, but still getting the same error, so I think is not considering this metadata when the INSERT query is being generated.

    The new partial class have this code:
    [MetadataType(typeof(OrderFileMetadata))]
        public partial class OrderFile
        {
            internal class OrderFileMetadata
            {
                [Column("ID", OpenAccessType = OpenAccessType.Int32, Length = 0, Scale = 0, SqlType = "int", IsBackendCalculated = true, IsPrimaryKey = true)]
                public int ID { get; set; }
     
     
                [Column("UniqueID", OpenAccessType = OpenAccessType.Guid, Length = 0, Scale = 0, SqlType = "uniqueidentifier")]
                public Guid UniqueID { get; set; }
            }
        }

    I appreciate any help for solving this issue.

    Regards,
    Tiago Salgado


  2. Dimitar Tachev
    Admin
    Dimitar Tachev avatar
    67 posts

    Posted 03 Oct 2013 Link to this post

    Hello Tiago,

     
    Based on the details that you provided us I suppose that the problematic class is part of a domain model using the attributes mapping.

    In this way the domain classes and their attributes are generated based on the setting configured in our Visual Designer.

    Using the Database First approach if the identity settings of the class haven't been changed after running the Add New Domain Model Wizard they should be the same as they are configured in the database. In other words the UniqueID property should be generated as a primary key only if it is set as a primary key in your database.

    Could you please confirm that the Id column is set as a primary key in the database table. If it is configured properly, could you please provide us with the database script (you can copy the script using the approach from the attached image) in order to investigate this issue further?

    If you need to change the identity of an existing domain class you can achieve that by configuring the proper value of the Identity setting for the properties of your domain classes. You could find more detailed information about how to set the identity of a domain class in this documentation section. After changing the Identity setting of some properties you just need to save the RLINQ file in order to trigger our code generation process and the new configuration should be applied.

    I am looking forward to your feedback.

    Regards,
    Dimitar Tachev
    Telerik
    OpenAccess ORM Q3 2013 Beta is available for immediate download in your account. Get it now and play with the latest bits. See what's new >>
  3. DevCraft banner
  4. Tiago
    Tiago avatar
    4 posts
    Member since:
    Nov 2012

    Posted 03 Oct 2013 Link to this post

    Hi,
    Yes, the uniqueID is a Primary Key, because I have another column as varbinary (FILESTREAM) so I think I need to had this kind of structure.

    The script for create table is this:
    CREATE TABLE [dbo].[OrderFiles](
        [ID] [int] IDENTITY(1,1) NOT NULL,
        [UniqueID] [uniqueidentifier] ROWGUIDCOL  NOT NULL,
        [OrderID] [int] NOT NULL,
        [DocumentTypeID] [tinyint] NOT NULL,
        [FileExtensionID] [smallint] NOT NULL,
        [FileName] [nvarchar](255) NOT NULL,
        [FileContent] [varbinary](max) FILESTREAM  NULL,
        [Instructions] [varchar](255) NULL,
        [FileExtensionTargetID] [smallint] NOT NULL,
        [DateUploaded] [datetime] NOT NULL,
        [ChronicleID] [char](16) NULL,
        [DocumentumFileId] [char](16) NULL,
     CONSTRAINT [PK_OrderFiles] PRIMARY KEY NONCLUSTERED
    (
        [UniqueID] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY] FILESTREAM_ON [FILESTREAM]
     
    GO
     
    SET ANSI_PADDING OFF
    GO
     
    ALTER TABLE [dbo].[OrderFiles] ADD  DEFAULT (newid()) FOR [UniqueID]
    GO

    That's why I want to change the default generated code, otherwise I can't insert new records.

    My problem is every time I need to update the Datamodel, I need to update this class again, so I'm trying to avoid that using a separate partial class with a proper column attribute properties.

    Regards,
    Tiago Salgado
  5. Dimitar Tachev
    Admin
    Dimitar Tachev avatar
    67 posts

    Posted 04 Oct 2013 Link to this post

    Hello Tiago,

     
    Thank you for the provided details.

    Based on this article I believe that the uniqueidentifier ROWGUID column is required by the FILESTREAM column but it is not necessary to be a primary key. 

    For this reason in order to achieve the database structure that you need I suggest you follow the steps below. 

    1) Recreate your OrderFiles table in order to set the ID column as a PRIMARY KEY and the UniqueID one as UNIQUE.

    CREATE TABLE [dbo].[OrderFiles](
        [ID] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY,
        [UniqueID] [uniqueidentifier] ROWGUIDCOL  NOT NULL UNIQUE,
        [OrderID] [int] NOT NULL,
        [DocumentTypeID] [tinyint] NOT NULL,
        [FileExtensionID] [smallint] NOT NULL,
        [FileName] [nvarchar](255) NOT NULL,
        [FileContent] [varbinary](max) FILESTREAM  NULL,
        [Instructions] [varchar](255) NULL,
        [FileExtensionTargetID] [smallint] NOT NULL,
        [DateUploaded] [datetime] NOT NULL,
        [ChronicleID] [char](16) NULL,
        [DocumentumFileId] [char](16) NULL,
    ) ON [PRIMARY] FILESTREAM_ON [FILESTREAM]
     
    GO
      
    SET ANSI_PADDING OFF
    GO
      
    ALTER TABLE [dbo].[OrderFiles] ADD  DEFAULT (newid()) FOR [UniqueID]
    GO

    2) In order to take advantage of the database default value of the UniqueID column, please follow the steps in our Handle Default Values article - I believe that you will need only to set the UniqueID property Nullability to true from the Visual Designer which will not cause any changes when you update the model from the database or vice versa.

    For your convenience I have also prepared a sample application demonstrating this approach and inserting a sample image in the database using our Stream API - please find it attached.

    If you need any further information about working with data streams using Telerik OpenAccess ORM, please take a look at this documentation section. You could also find a detailed example of its usage in our Streaming Videos using OpenAccess example from the API section of our Samples Kit demonstrating a chunked (in separated parts) read and upload of video files.

    I hope this is applicable for you. Do not hesitate to contact us back if you need any further assistance.


    Regards,
    Dimitar Tachev
    Telerik
    OpenAccess ORM Q3 2013 Beta is available for immediate download in your account. Get it now and play with the latest bits. See what's new >>
Back to Top