SetValueAttribute Class
.NET Standard 2.x
Specifies that when there is an insert or update operation in the master model, set the property value of the master model to the associated property of the detail model, when working with the master-detail models. One property is allowed to apply multiple SetValue
attributes.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Inheritance Constructor
System.Attribute
Syntax
[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
public class SetValueAttribute : Attribute, ISetValue
Constructors
Name | Description |
---|---|
SetValueAttribute( string source,string target) | Initializes a new instance of the SetValueAttribute class. |
SetValueAttribute(string source,string target,SetValueStrategy setValueStrategy) | Initializes a new instance of the SetValueAttribute class. |
SetValueAttribute(string source, string target,SetValueStrategy setValueStrategy,ModelSelector modelSelector) | Initializes a new instance of the SetValueAttribute class. |
Properties
Name | Return Type | Description |
---|---|---|
ModelSelector | ModelSelector | Gets the ModelSelector enumerated value which determines whether to set value by the data state in the embedded model. |
SetValueStrategy | SetValueStrategy | Gets the SetValueStrategy enumerated value which determines when to set value. |
Source | string | Gets the string that represents a property in the model class at the same level. |
Target | string | Gets the string that represents a property in the model class at one level lower. |
Remarks
The SetValue
attribute must be used together with the ModelEmbedded attribute.
Examples
Example 1
The following code example demonstrates how to save the detail model when SetValueStrategy
is Always
.
Model: CourseSetValueExample1
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using SnapObjects.Data;
using Newtonsoft.Json;
namespace Appeon.ApiDoc.Models.School
{
/// <summary>
/// This model class maps to the dbo.Course table.
/// It uses the primary key to generate the Where clause for the Update operation.
///
/// CourseID is the primary key.
[Table("Course", Schema = "dbo")]
[UpdateWhereStrategy(UpdateWhereStrategy.KeyColumns)]
public class CourseSetValueExample1
{
[Key]
public int CourseID { get; set; }
public string Title { get; set; }
public int Credits { get; set; }
public int DepartmentID { get; set; }
[JsonIgnore]
[SetValue("$CourseID", "$CourseID", SetValueStrategy.Always)]
[ModelEmbedded(typeof(OnlineCourse), ParamValue = "$CourseID",
CascadeCreate = true, CascadeDelete = true)]
public OnlineCourse onlineCourse { get; set; }
}
}
Model: OnlineCourse
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.OnlineCourse table.
/// It uses the primary key to generate the Where clause for the Update operation.
///
/// CourseID is the primary key.
/// </summary>
[Table("OnlineCourse", Schema = "dbo")]
[UpdateWhereStrategy(UpdateWhereStrategy.KeyColumns)]
public class OnlineCourse
{
[Key]
public int CourseID { get; set; }
public string URL { get; set; }
}
}
Example Method: Example1
using SnapObjects.Data;
using Appeon.ApiDoc.Models.School;
using System;
using System.IO;
using System.Linq;
namespace Appeon.ApiDoc.SetValueAttributeExamples
{
public class SetValueAttributeExample
{
private readonly SchoolContext _context;
public SetValueAttributeExample(SchoolContext dataContext)
{
_context = dataContext;
}
public void Example1(IModelEntry<CourseSetValueExample1> course,
IModelEntry<OnlineCourse> onlineCourse)
{
var mapper = _context.SqlModelMapper;
/// Course is set as the master model, and OnlineCourse is set as the detail model.
/// In the definition of the detail model, apply the SetValue attribute
/// to synchronize the CourseID for OnlineCourse and Course,
/// and set SetValueStrategy to Always: SetValueStrategy.Always.
var master = mapper.TrackMaster(course)
.TrackDetail(m => m.onlineCourse, onlineCourse)
.MasterModel;
// Shows the CourseID before saving Course and OnlineCourse.
var courseID = master.CourseID;
var onlineCourseID = master.onlineCourse.CourseID;
Console.WriteLine("Before Save: ");
Console.WriteLine("Course.CourseID = {0}", courseID);
Console.WriteLine("OnlineCourse.CourseID = {0}", onlineCourseID);
// Saves data.
var dbResult = mapper.SaveChanges();
// Shows the CourseID after saving Course and OnlineCourse.
courseID = master.CourseID;
onlineCourseID = master.onlineCourse.CourseID;
Console.WriteLine("After Save: ");
Console.WriteLine("Course.CourseID = {0}", courseID);
Console.WriteLine("OnlineCourse.CourseID = {0}", onlineCourseID);
/* This code example produces the following output:
Before Save:
Course.CourseID = 8001
OnlineCourse.CourseID = 0
After Save:
Course.CourseID = 8001
OnlineCourse.CourseID = 8001
*/
}
}
}
Example 2
The following code example demonstrates how to save the detail model when SetValueStrategy is AfterParentCreated.
Model: OnlineCourse
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.OnlineCourse table.
/// It uses the primary key to generate the Where clause for the Update operation.
///
/// CourseID is the primary key.
/// </summary>
[Table("OnlineCourse", Schema = "dbo")]
[UpdateWhereStrategy(UpdateWhereStrategy.KeyColumns)]
public class OnlineCourse
{
[Key]
public int CourseID { get; set; }
public string URL { get; set; }
}
}
Model: CourseSetValueExample2
using SnapObjects.Data;
using Newtonsoft.Json;
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace Appeon.ApiDoc.Models.School
{
[Table("Course", Schema = "dbo")]
[UpdateWhereStrategy(UpdateWhereStrategy.KeyColumns)]
public class CourseSetValueExample2
{
[Key]
public int CourseID { get; set; }
public string Title { get; set; }
public int Credits { get; set; }
public int DepartmentID { get; set; }
[JsonIgnore]
[SetValue("$CourseID", "$CourseID", SetValueStrategy.AfterParentCreated)]
[ModelEmbedded(typeof(OnlineCourse), ParamValue = "$CourseID",
CascadeCreate = true, CascadeDelete = true)]
public OnlineCourse onlineCourse { get; set; }
}
}
Example Method: Example2
public void Example2(IModelEntry<CourseSetValueExample2> course,
IModelEntry<OnlineCourse> onlineCourse)
{
var mapper = _context.SqlModelMapper;
/// Course is set as the master model, and OnlineCourse is set as the detail model.
/// In the definition of the detail model, apply the SetValue attribute
/// to synchronize the CourseID for OnlineCourse and Course, and set
/// SetValueStrategy to AfterParentCreated: SetValueStrategy.AfterParentCreated.
var master = mapper.TrackMaster(course)
.TrackDetail(m => m.onlineCourse, onlineCourse)
.MasterModel;
// Shows the CourseID before saving Course and OnlineCourse.
var courseID = master.CourseID;
var onlineCourseID = master.onlineCourse.CourseID;
Console.WriteLine("Before Save: ");
Console.WriteLine("Course.CourseID = {0}", courseID);
Console.WriteLine("OnlineCourse.CourseID = {0}", onlineCourseID);
// Saves data.
var dbResult = mapper.SaveChanges();
// Shows the CourseID after saving Course and OnlineCourse.
courseID = master.CourseID;
onlineCourseID = master.onlineCourse.CourseID;
Console.WriteLine("After Save: ");
Console.WriteLine("Course.CourseID = {0}", courseID);
Console.WriteLine("OnlineCourse.CourseID = {0}", onlineCourseID);
/* This code example produces the following output:
Before Save:
Course.CourseID = 8001
OnlineCourse.CourseID = 0
After Save:
Course.CourseID = 8001
OnlineCourse.CourseID = 8001
*/
}
Example 3
The following code example demonstrates how to save the detail model when SetValueStrategy is AfterParentUpdated.
Model: OnlineCourse
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.OnlineCourse table.
/// It uses the primary key to generate the Where clause for the Update operation.
///
/// CourseID is the primary key.
/// </summary>
[Table("OnlineCourse", Schema = "dbo")]
[UpdateWhereStrategy(UpdateWhereStrategy.KeyColumns)]
public class OnlineCourse
{
[Key]
public int CourseID { get; set; }
public string URL { get; set; }
}
}
Model: CourseSetValueExample3
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using SnapObjects.Data;
using Newtonsoft.Json;
using System.Collections.Generic;
namespace Appeon.ApiDoc.Models.School
{
[Table("Course", Schema = "dbo")]
[UpdateWhereStrategy(UpdateWhereStrategy.KeyColumns)]
public class CourseSetValueExample3
{
[Key]
public int CourseID { get; set; }
public string Title { get; set; }
public int Credits { get; set; }
public int DepartmentID { get; set; }
[JsonIgnore]
[SetValue("Title", "URL", SetValueStrategy.AfterParentUpdated)]
[ModelEmbedded(typeof(OnlineCourse), ParamValue = "$CourseID",
CascadeCreate = true, CascadeDelete = true)]
public OnlineCourse onlineCourse { get; set; }
}
}
Example Method: Example3
public void Example3(IModelEntry<CourseSetValueExample3> course,
IModelEntry<OnlineCourse> onlineCourse)
{
var mapper = _context.SqlModelMapper;
/// Course is set as the master model, and OnlineCourse is set as the detail model.
/// In the definition of the detail model, apply the SetValue attribute
/// to synchronize the OnlineCourse.URL for OnlineCourse and Course, and set
/// SetValueStrategy to AfterParentUpdated: SetValueStrategy.AfterParentUpdated.
/// AfterParentUpdated can set the property values for the detail model after
/// the master model is saved. AfterParentUpdated can only save the updated data.
/// When AfterParentUpdated is used, it is not recommended to set values for primary key.
var master = mapper.TrackMaster(course)
.TrackDetail(m => m.onlineCourse, onlineCourse)
.MasterModel;
// Shows the Course.Title and OnlineCourse.URL before they are saved.
var title = master.Title;
var url = master.onlineCourse.URL;
Console.WriteLine("Before Save: ");
Console.WriteLine("Course.Title = {0}", title);
Console.WriteLine("OnlineCourse.URL = {0}", url);
// Saves data.
var dbResult = mapper.SaveChanges();
// Shows the Course.Title and OnlineCourse.URL after they were saved.
title = master.Title;
url = master.onlineCourse.URL;
Console.WriteLine("After Save: ");
Console.WriteLine("Course.Title = {0}", title);
Console.WriteLine("OnlineCourse.URL = {0}", url);
/* This code example produces the following output:
Before Save:
Course.Title = Test Course
OnlineCourse.URL = http://
After Save:
Course.Title = Test Course
OnlineCourse.URL = Test Course
*/
}
Applies to
.NET Standard
2.x