Show / Hide Table of Contents

    DwKeyModificationAttribute Class

    .NET Standard 2.x

    Specifies which SQL statement will be generated when the primary key changes, corresponding to the Key Modification defined by DataWindow.sqlStrategy.

    Namespace: DWNet.Data

    Assembly: DWNet.Data.dll

    Inheritance Constructor

    System.Attribute

    Syntax

        [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
        public class DwKeyModificationStrategyAttribute : Attribute
    

    Constructors

    Name Description
    DwKeyModificationStrategyAttribute(UpdateSqlStrategy sqlStrategy) Initializes a new instance of the KeyModificationAttribute class.

    Properties

    Name Return Type Description
    SqlStrategy UpdateSqlStrategy Which SQL statement to be generated when the primary key changes.

    Remarks

    The options are:

    DeleteThenInsert: Deletes the current row and then inserts a new row.

    Update: Directly updates the current row.

    Examples

    The following code example demonstrates how to use the Model DwKeyModificationStrategy attribute. The example defines DwKeyModificationStrategy which uses the UpdateSqlStrategy.DeleteThenInsert parameter, which means two SQL statements will be generated when updating the primary key, one is the Delete statement for deleting data, the other is the Insert statement for inserting new data.

    Model: D_Onlinecourse_Modifiykey
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using SnapObjects.Data;
    using DWNet.Data;
    using Newtonsoft.Json;
    using System.Collections;
    
    namespace Appeon.ApiDoc.Models
    {
        [DataWindow("d_onlinecourse_modifiykey", DwStyle.Grid)]
        [Table("OnlineCourse")]
        #region DwSelectAttribute  
        [DwSelect("PBSELECT( VERSION(400) TABLE(NAME=\"OnlineCourse\" ) @(_COLUMNS_PLACEHOLDER_) )")]
        #endregion
        [DwKeyModificationStrategy(UpdateSqlStrategy.DeleteThenInsert)]
        [UpdateWhereStrategy(UpdateWhereStrategy.KeyColumns)]
        public class D_Onlinecourse_Modifiykey
        {
            [Key]
            [DwColumn("OnlineCourse", "CourseID")]
            public int Courseid { get; set; }
    
            [StringLength(100)]
            [DwColumn("OnlineCourse", "URL")]
            public string Url { get; set; }
    
        }
    
    }
    
    Example Method:
    using System;
    using DWNet.Data;
    
    namespace Appeon.ApiDoc.DwKeyModificationStrategyAttributeExamples
    {
        public class DwKeyModificationStrategyAttributeExample
        {
            private readonly SchoolContext _context;
    
            public DwKeyModificationStrategyAttributeExample(SchoolContext dataContext)
            {
                // Sets the data context.
                _context = dataContext;
            }
    
            public void Example()
            {
                var datastore = new DataStore("d_onlinecourse_modifiykey", _context);
                datastore.Retrieve();
    
                //Sets the Courseid in the first row to 1045.
                datastore.SetItem(0, "Courseid", 1045);
                
    			//Commits modifications to the DataStore.
    			datastore.Update();
    
                /* The following SQL is generated:
                
                exec sp_executesql N'DELETE FROM [OnlineCourse] 
                WHERE [CourseID] = @p_p0;
                SELECT @@ROWCOUNT;
    
                ',N'@p_p0 int',@p_p0=2021
    
                exec sp_executesql N'INSERT INTO [OnlineCourse] ([CourseID], [URL])
                VALUES (@pp0, @pp1);
                SELECT @@ROWCOUNT;
    
                ',N'@pp0 int,@pp1 nvarchar(4000)',@pp0=1045,@pp1=N'http://www.fineartschool.net/Composition'
                */
            }
        }
    }
    

    Example Refer To

    DataWindow File: d_onlinecourse_modifiykey

    Back to top Generated by Appeon