UpdateWhereStrategyAttribute Class
.NET Standard 2.x
Specifies the column(s) to be used as search condition when the model class is performing updates to the database.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Inheritance Constructor
System.Attribute
Syntax
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
public class UpdateWhereStrategyAttribute : Attribute, IUpdateWhereStrategy
Constructors
Name | Description |
---|---|
UpdateWhereStrategyAttribute(UpdateWhereStrategy strategy) | Initializes a new instance of the UpdateWhereStrategyAttribute class. |
Properties
Name | Return Type | Description |
---|---|---|
UpdateWhereStrategy | UpdateWhereStrategy | Gets the strategy that which column(s) will be included in the WHERE clause of the UPDATE statement. |
Remarks
The UpdateWhereStrategy
attribute indicates the strategy that which column(s) will be included in the WHERE clause of the SQL UPDATE statement for a model class.
If no UpdateWhereStrategy
attribute is applied to a model class, the enumeration value UpdateWhereStrategy.KeyColumns
(WHERE statement only includes the key columns) will be used by default.
Examples
Example 1
The following code example demonstrates how to use the UpdateWhereStrategy attribute. It applies the UpdateWhereStrategy.KeyAndModifiedColumns strategy to the DepartmentUpdateWhereStrategy class. When the SQL UPDATE statement is generated, it uses the primary key and the modified properties to create the WHERE clause.
Model: DepartmentUpdateWhereStrategy
using SnapObjects.Data;
using System;
using System.ComponentModel.DataAnnotations;
namespace Appeon.ApiDoc.Models.School
{
/// <summary>
/// This model class maps to the dbo.Department table.
/// </summary>
[FromTable("Department", Schema = "dbo")]
[UpdateWhereStrategy(UpdateWhereStrategy.KeyAndModifiedColumns)]
public class DepartmentUpdateWhereStrategy
{
[Key]
public int DepartmentID { get; set; }
public string Name { get; set; }
public decimal Budget { get; set; }
[ConcurrencyCheck]
public DateTime StartDate { get; set; }
public int? Administrator { get; set; }
}
}
Example Method: Example1
using Appeon.ApiDoc.Models.School;
namespace Appeon.ApiDoc.UpdateWhereStrategyAttributeExamples
{
public class UpdateWhereStrategyAttributeExample
{
private SchoolContext _context;
public UpdateWhereStrategyAttributeExample(SchoolContext dataContext)
{
// Sets the data context.
_context = dataContext;
}
public void Example1()
{
var mapper = _context.SqlModelMapper;
// Gets the department record of Engineering (DepartmentID = 1)
// from the database.
var department = mapper.LoadByKey<DepartmentUpdateWhereStrategy>(1)
.FirstOrDefault();
mapper.TrackUpdate(department);
// Changes the department name from 'Engineering' to 'New Department'
department.Name = "New Department";
mapper.SaveChanges();
/* Internally generated T-SQL:
exec sp_executesql N'UPDATE [dbo].[Department] SET [Name] = @p0
WHERE [DepartmentID] = @_p1 AND [Name] = @_p2;
SELECT @@ROWCOUNT;
',N'@_p1 int,@p0 nvarchar(4000),@_p2 nvarchar(4000)',
@_p1=1,@p0=N'New Department',@_p2=N'Engineering'
*/
}
}
}
Example 2
The following code example demonstrates how to use the UpdateWhereStrategy attribute. It applies the UpdateWhereStrategy.Key strategy to the DepartmentUpdateWhereStrategy class. When the SQL UPDATE statement is generated, it uses only the primary key to create the WHERE clause.
Model: DepartmentUpdateWhereStrategy2
using SnapObjects.Data;
using System;
using System.ComponentModel.DataAnnotations;
namespace Appeon.ApiDoc.Models.School
{
/// <summary>
/// This model class maps to the dbo.Department table.
/// </summary>
[FromTable("Department", Schema = "dbo")]
[UpdateWhereStrategy(UpdateWhereStrategy.KeyColumns)]
public class DepartmentUpdateWhereStrategy2
{
[Key]
public int DepartmentID { get; set; }
public string Name { get; set; }
public decimal Budget { get; set; }
[ConcurrencyCheck]
public DateTime StartDate { get; set; }
public int? Administrator { get; set; }
}
}
Example Method: Example2
public void Example2()
{
var mapper = _context.SqlModelMapper;
// Gets the department record of Engineering (DepartmentID = 1)
// from the database.
var department = mapper.LoadByKey<DepartmentUpdateWhereStrategy2>(1)
.FirstOrDefault();
mapper.TrackUpdate(department);
// Changes the department name from 'Engineering' to 'New Department'.
department.Name = "New Department";
mapper.SaveChanges();
/* Internally generated T-SQL:
exec sp_executesql N'UPDATE [dbo].[Department] SET [Name] = @p0
WHERE [DepartmentID] = @_p1;
SELECT @@ROWCOUNT;
',N'@_p1 int,@p0 nvarchar(4000)',@_p1=1,@p0=N'New Department'
*/
}
Example 3
The following code example demonstrates how to use the UpdateWhereStrategy attribute to set UpdateWhereStrategy.Key strategy to the DepartmentUpdateWhereStrategy class. When the SQL UPDATE statement is generated, it uses the primary key and the properties which have applied the ConcurrencyCheck attribute to create the WHERE clause.
Model: DepartmentUpdateWhereStrategy3
using SnapObjects.Data;
using System;
using System.ComponentModel.DataAnnotations;
namespace Appeon.ApiDoc.Models.School
{
/// <summary>
/// This model class maps to the dbo.Department table.
/// </summary>
[FromTable("Department", Schema = "dbo")]
[UpdateWhereStrategy(UpdateWhereStrategy.KeyAndConcurrencyCheckColumns)]
public class DepartmentUpdateWhereStrategy3
{
[Key]
public int DepartmentID { get; set; }
public string Name { get; set; }
public decimal Budget { get; set; }
[ConcurrencyCheck]
public DateTime StartDate { get; set; }
public int? Administrator { get; set; }
}
}
Example Method: Example3
public void Example3()
{
var mapper = _context.SqlModelMapper;
// Gets the department record of Engineering (DepartmentID = 1)
// from the database.
var department = mapper.LoadByKey<DepartmentUpdateWhereStrategy3>(1)
.FirstOrDefault();
mapper.TrackUpdate(department);
// Changes the department name from 'Engineering' to 'New Department'.
department.Name = "New Department";
mapper.SaveChanges();
/* Internally generated T-SQL:
exec sp_executesql N'UPDATE [dbo].[Department] SET [Name] = @p0
WHERE [DepartmentID] = @_p1 AND [StartDate] = @_p2;
SELECT @@ROWCOUNT;
',N'@_p1 int,@p0 nvarchar(4000),@_p2 datetime2(7)',@_p1=1,
@p0=N'Engineering_test', @_p2='2007-09-01 00:00:00'
*/
}
Applies to
.NET Standard
2.x