PropertySaveAttribute Class
.NET Standard 2.x
Specifies the strategy to be used when saving data for a property to the database.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Inheritance Constructor
System.Attribute
Syntax
[AttributeUsage(AttributeTargets.Property)]
public class PropertySaveAttribute : Attribute, IPropertySave
Constructors
Name | Description |
---|---|
PropertySaveAttribute(SaveStrategy saveStrategy) | Initializes a new instance of the PropertySaveAttribute class. |
Properties
Name | Return Type | Description |
---|---|---|
SaveStrategy | SaveStrategy | Gets the strategy that is used when saving data for the current property to the database. |
Examples
Example 1
The following code example demonstrates how to use the PropertySave
attribute to specify the SaveStrategy.Ignore
strategy.
When the Description property in the CourseDescriptionPropertySave class is applied with the PropertySave attribute using the SaveStrategy.Ignore strategy, it indicates when that data in the model (CourseDescriptionPropertySave) is modified or created, value of that property will NOT be updated to the database.
Model: CourseDescriptionPropertySave
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using SnapObjects.Data;
namespace Appeon.ApiDoc.Models.School
{
/// <summary>
/// This model class maps to the dbo.CourseDescription table.
///
/// [PropertySave(SaveStrategy.Ignore)]
///
/// Ignores the inserted/updated Description column.
/// </summary>
[Table("CourseDescription", Schema = "dbo")]
public class CourseDescriptionPropertySave
{
[Key]
public int CourseID { get; set; }
[PropertySave(SaveStrategy.Ignore)]
public string Description { get; set; }
public DateTime ModifiedDate { get; set; }
public Guid? rowguid { get; set; }
}
}
Example Method: Example1
using Appeon.ApiDoc.Models.School;
using System;
namespace Appeon.ApiDoc.PropertySaveAttributeExamples
{
public class PropertySaveAttributeExample
{
private SchoolContext _context;
public PropertySaveAttributeExample(SchoolContext dataContext)
{
// Sets the data context.
_context = dataContext;
}
public void Example1()
{
var mapper = _context.SqlModelMapper;
DateTime date = DateTime.Parse("2018/01/01");
// Changes an existing courseDescription record.
var courseDescription = mapper.LoadByKey<CourseDescriptionPropertySave>(1045)
.FirstOrDefault();
mapper.TrackUpdate(courseDescription);
courseDescription.Description = "Calculus Description New";
courseDescription.ModifiedDate = date;
mapper.SaveChanges();
/* Although the Description value is changed, the Description field is
set to SaveStrategy.Ignore, therefore the changed value of Description
will NOT be saved to the database when SaveChanges is executed.
The following UPDATE SQL is generated:
exec sp_executesql N'UPDATE [dbo].[CourseDescription]
SET [ModifiedDate] = @p0
WHERE [CourseID] = @_p1;
SELECT @@ROWCOUNT;
',N'@_p1 int,@p0 datetime2(7)',@_p1=1045,@p0='2018-01-01 00:00:00'
*/
// Adds a courseDescription record.
var courseDescription2 = new CourseDescriptionPropertySave();
courseDescription2.CourseID = 1050;
courseDescription2.Description = "Calculus Description New";
courseDescription2.ModifiedDate = date;
mapper.TrackCreate(courseDescription2).SaveChanges();
/* Although the Description field is assigned with a value when created,
the Description field is set to SaveStrategy.Ignore, therefore the
Description value will NOT be saved to the database when SaveChanges is executed.
The following INSERT SQL is generated:
exec sp_executesql N'INSERT INTO [dbo].[CourseDescription]
([CourseID], [ModifiedDate])
VALUES (@p0, @p1);
SELECT @@ROWCOUNT;
',N'@p0 int,@p1 datetime2(7)',@p0=1050,@p1='2018-01-01 00:00:00'
*/
}
}
}
Example 2
The following code example demonstrates how to use the PropertySave attribute to specify the SaveStrategy.ReadAfterSave strategy.
When the rowguid property in the CourseDescriptionPropertySave2 class is applied with the PropertySave attribute using the SaveStrategy.ReadAfterSave strategy, it indicates when that data in the model (CourseDescriptionPropertySave2) is modified or created, value of that property will NOT be updated to the database; but value in the database will be retrieved after SaveChanges is executed.
Model: CourseDescriptionPropertySave2
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using SnapObjects.Data;
namespace Appeon.ApiDoc.Models.School
{
/// <summary>
/// This model class maps to the dbo.CourseDescription table.
///
/// [PropertySave(SaveStrategy.ReadAfterSave)]
///
/// The value of the rowguid column is created in the database.
/// </summary>
[Table("CourseDescription", Schema = "dbo")]
public class CourseDescriptionPropertySave2
{
[Key]
public int CourseID { get; set; }
public string Description { get; set; }
public DateTime ModifiedDate { get; set; }
[PropertySave(SaveStrategy.ReadAfterSave)]
public Guid? rowguid { get; set; }
}
}
Example Method: Example2
public void Example2()
{
var mapper = _context.SqlModelMapper;
DateTime date = DateTime.Parse("2018/01/01");
var guid = Guid.NewGuid();
// Changes an existing courseDescription record.
var courseDescription = mapper
.LoadByKey<CourseDescriptionPropertySave2>(1045)
.FirstOrDefault();
mapper.TrackUpdate(courseDescription);
courseDescription.Description = "Calculus Description New";
courseDescription.ModifiedDate = date;
courseDescription.rowguid = guid;
mapper.SaveChanges();
Console.WriteLine("setting rowguid:{0}", guid.ToString());
Console.WriteLine("getting rowguid:{0}", courseDescription.rowguid);
/* Although the rowguid value is changed, the rowguid field is
set to SaveStrategy.ReadAfterSave, therefore the changed value of rowguid
will NOT be saved to the database when SaveChanges is executed; and
courseDescription.rowguid will NOT be updated with the value in the database
after SaveChanges is executed.
The following UPDATE SQL is generated:
exec sp_executesql N'UPDATE [dbo].[CourseDescription]
SET [Description] = @p0, [ModifiedDate] = @p1
WHERE [CourseID] = @_p2;
SELECT @@ROWCOUNT;
',N'@_p2 int,@p0 nvarchar(4000),@p1 datetime2(7)',
@_p2=1045,@p0=N'Calculus Description New',@p1='2018-01-01 00:00:00'
*/
// Adds a courseDescription record.
var courseDescription2 = new CourseDescriptionPropertySave2();
courseDescription2.CourseID = 1050;
courseDescription2.Description = "Calculus Description New";
courseDescription2.ModifiedDate = date;
guid = Guid.NewGuid();
courseDescription.rowguid = guid;
mapper.TrackCreate(courseDescription2).SaveChanges();
Console.WriteLine("setting rowguid: {0}", guid.ToString());
Console.WriteLine("getting rowguid: {0}", courseDescription2.rowguid);
/* Although the rowguid field is assigned with a value when created,
the rowguid field is set to SaveStrategy.ReadAfterSave, therefore the
value of rowguid will NOT be saved to the database when SaveChanges is
executed; but courseDescription.rowguid will be updated with the value
in the database after SaveChanges is executed.
The following INSERT SQL is generated:
exec sp_executesql N'INSERT INTO [dbo].[CourseDescription]
([CourseID], [Description], [ModifiedDate])
VALUES (@p0, @p1, @p2);
SELECT [rowguid]
FROM [dbo].[CourseDescription]
WHERE @@ROWCOUNT = 1 AND [CourseID] = @p0;
',N'@p0 int,@p1 nvarchar(4000),@p2 datetime2(7)',
@p0=1050,@p1=N'Calculus Description New',@p2='2018-01-01 00:00:00'
/* This code example produces the following output:
setting rowguid: fd696a2b-e0d9-4e98-b442-8a472f92e2d7
getting rowguid: fd696a2b-e0d9-4e98-b442-8a472f92e2d7
setting rowguid: efd18a0e-0e6f-4ee7-9d9c-4cbf280b2303
getting rowguid: bb00f3b1-1c9b-47fe-9fc9-3a1895475ff3
*/
}
Example 3
The following code example demonstrates how to use the PropertySave attribute to specify the SaveStrategy.Save strategy.
When the Description property in the CourseDescriptionPropertySave3 class is applied with the PropertySave attribute using the SaveStrategy.Save strategy, it indicates when that data in the model (CourseDescriptionPropertySave3) is modified or created, value of that property will be updated to the database when SaveChanges is executed.
Model: CourseDescriptionPropertySave3
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using SnapObjects.Data;
namespace Appeon.ApiDoc.Models.School
{
/// <summary>
/// This model class maps to the dbo.CourseDescription table.
///
/// [PropertySave(SaveStrategy.Save)]
///
/// The Description column is applied with [PropertySave(SaveStrategy.Save)].
/// </summary>
[Table("CourseDescription", Schema = "dbo")]
public class CourseDescriptionPropertySave3
{
[Key]
public int CourseID { get; set; }
[PropertySave(SaveStrategy.Save)]
public string Description { get; set; }
public DateTime ModifiedDate { get; set; }
public Guid? rowguid { get; set; }
}
}
Example Method: Example3
public void Example3()
{
var mapper = _context.SqlModelMapper;
DateTime date = DateTime.Parse("2018/01/01");
// Changes an existing courseDescription record.
var courseDescription = mapper
.LoadByKey<CourseDescriptionPropertySave3>(1045)
.FirstOrDefault();
mapper.TrackUpdate(courseDescription);
courseDescription.Description = "Calculus Description New";
courseDescription.ModifiedDate = date;
mapper.SaveChanges();
/* The Description value is changed, and the Description field is
set to SaveStrategy.Save, therefore the changed value of Description
will be saved to the database when SaveChanges is executed.
The following UPDATE SQL is generated:
exec sp_executesql N'UPDATE [dbo].[CourseDescription]
SET [Description] = @p0, [ModifiedDate] = @p1
WHERE [CourseID] = @_p2;
SELECT @@ROWCOUNT;
',N'@_p2 int,@p0 nvarchar(4000),@p1 datetime2(7)',
@_p2=1045,@p0=N'Calculus Description New',@p1='2018-01-01 00:00:00'
*/
// Adds a courseDescription record.
var courseDescription2 = new CourseDescriptionPropertySave3();
courseDescription2.CourseID = 1050;
courseDescription2.Description = "Calculus Description New";
courseDescription2.ModifiedDate = date;
mapper.TrackCreate(courseDescription2).SaveChanges();
/* The Description field is assigned with a value when created,
and the Description field is set to SaveStrategy.Save, therefore
the Description value will be saved to the database when SaveChanges
is executed.
The following INSERT SQL is generated:
exec sp_executesql N'INSERT INTO [dbo].[CourseDescription]
([CourseID], [Description], [ModifiedDate])
VALUES (@p0, @p1, @p2);
SELECT @@ROWCOUNT;
',N'@p0 int,@p1 nvarchar(4000),@p2 datetime2(7)',
@p0=1050,@p1=N'Calculus Description New',@p2='2018-01-01 00:00:00'
*/
}
Example 4
The following code example demonstrates how to use the PropertySave attribute to specify the SaveStrategy.SaveOnCreate strategy.
When the Description property in the CourseDescriptionPropertySave4 class is applied with the PropertySave attribute using the SaveStrategy.SaveOnCreate strategy, it indicates when that model (CourseDescriptionPropertySave4) is modified, value of the Description property will NOT be updated to the database when SaveChanges is executed; but when that model is created, value of that property will be saved to the database when SaveChanges is executed.
Model: CourseDescriptionPropertySave4
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using SnapObjects.Data;
namespace Appeon.ApiDoc.Models.School
{
/// <summary>
/// This model class maps to the dbo.CourseDescription table.
///
/// [PropertySave(SaveStrategy.SaveOnCreate)]
///
/// The Description column is applied with [PropertySave(SaveStrategy.SaveOnCreate)].
/// </summary>
[Table("CourseDescription", Schema = "dbo")]
public class CourseDescriptionPropertySave4
{
[Key]
public int CourseID { get; set; }
[PropertySave(SaveStrategy.SaveOnCreate)]
public string Description { get; set; }
public DateTime ModifiedDate { get; set; }
public Guid? rowguid { get; set; }
}
}
Example Method: Example4
public void Example4()
{
var mapper = _context.SqlModelMapper;
DateTime date = DateTime.Parse("2018/01/01");
// Changes an existing courseDescription record.
var courseDescription = mapper
.LoadByKey<CourseDescriptionPropertySave4>(1045)
.FirstOrDefault();
mapper.TrackUpdate(courseDescription);
courseDescription.Description = "Calculus Description New";
courseDescription.ModifiedDate = date;
mapper.SaveChanges();
/* Although the Description value is changed, the Description field is
set to SaveStrategy.SaveOnCreate, therefore the changed value of Description
will NOT be updated to the database when SaveChanges is executed.
The following UPDATE SQL is generated:
exec sp_executesql N'UPDATE [dbo].[CourseDescription]
SET [ModifiedDate] = @p0
WHERE [CourseID] = @_p1;
SELECT @@ROWCOUNT;
',N'@_p1 int,@p0 datetime2(7)',@_p1=1045,@p0='2018-01-01 00:00:00'
*/
// Adds a courseDescription record.
var courseDescription2 = new CourseDescriptionPropertySave4();
courseDescription2.CourseID = 1050;
courseDescription2.Description = "Calculus Description New";
courseDescription2.ModifiedDate = date;
mapper.TrackCreate(courseDescription2).SaveChanges();
/* The Description field is assigned with a value when created, and the
Description field is set to SaveStrategy.SaveOnCreate, therefore the
Description value will be saved to the database when SaveChanges is executed.
The following INSERT SQL will be generated:
exec sp_executesql N'INSERT INTO
[dbo].[CourseDescription] ([CourseID], [Description], [ModifiedDate])
VALUES (@p0, @p1, @p2);
SELECT @@ROWCOUNT;
',N'@p0 int,@p1 nvarchar(4000),@p2 datetime2(7)',
@p0=1050,@p1=N'Calculus Description New',@p2='2018-01-01 00:00:00'
*/
}
Example 5
The following code example demonstrates how to use the PropertySave attribute to specify the SaveStrategy.SaveOnUpdate strategy.
When the Description property in the CourseDescriptionPropertySave5 class is applied with the PropertySave attribute using the SaveStrategy.SaveOnUpdate strategy, it indicates when that model (CourseDescriptionPropertySave5) is modified, value of the Description property will be updated to the database when SaveChanges is executed; but when that model is created, value of that property will NOT be saved to the database when SaveChanges is executed.
Model: CourseDescriptionPropertySave5
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using SnapObjects.Data;
namespace Appeon.ApiDoc.Models.School
{
/// <summary>
/// This model class maps to the dbo.CourseDescription table.
///
/// [PropertySave(SaveStrategy.SaveOnUpdate)]
///
/// The Description column is applied with [PropertySave(SaveStrategy.SaveOnUpdate)].
/// </summary>
[Table("CourseDescription", Schema = "dbo")]
public class CourseDescriptionPropertySave5
{
[Key]
public int CourseID { get; set; }
[PropertySave(SaveStrategy.SaveOnUpdate)]
public string Description { get; set; }
public DateTime ModifiedDate { get; set; }
public Guid? rowguid { get; set; }
}
}
Example Method: Example5
public void Example5()
{
var mapper = _context.SqlModelMapper;
DateTime date = DateTime.Parse("2018/01/01");
// Changes an existing courseDescription record.
var courseDescription = mapper
.LoadByKey<CourseDescriptionPropertySave5>(1045)
.FirstOrDefault();
mapper.TrackUpdate(courseDescription);
courseDescription.Description = "Calculus Description New";
courseDescription.ModifiedDate = date;
mapper.SaveChanges();
/* The Description value is changed, and the Description field is
set to SaveStrategy.SaveOnUpdate, therefore the changed value of Description
will be updated to the database when SaveChanges is executed.
The following UPDATE SQL is generated:
exec sp_executesql N'UPDATE [dbo].[CourseDescription]
SET [Description] = @p0, [ModifiedDate] = @p1
WHERE [CourseID] = @_p2;
SELECT @@ROWCOUNT;
',N'@_p2 int,@p0 nvarchar(4000),@p1 datetime2(7)',
@_p2=1045,@p0=N'Calculus Description New',@p1='2018-01-01 00:00:00'
*/
// Adds a courseDescription record.
var courseDescription2 = new CourseDescriptionPropertySave5();
courseDescription2.CourseID = 1050;
courseDescription2.Description = "Calculus Description New";
courseDescription2.ModifiedDate = date;
mapper.TrackCreate(courseDescription2).SaveChanges();
/* The Description field is assigned with a value when created, but the
Description field is set to SaveStrategy.SaveOnUpdate, therefore the
Description value will NOT be saved to the database when SaveChanges is executed.
The following INSERT SQL is generated:
exec sp_executesql N'INSERT INTO
[dbo].[CourseDescription] ([CourseID], [ModifiedDate])
VALUES (@p0, @p1);
SELECT @@ROWCOUNT;
',N'@p0 int,@p1 datetime2(7)',@p0=1050,@p1='2018-01-01 00:00:00'
*/
}
Applies to
.NET Standard
2.x