SqlSelectAttribute Class
.NET Standard 2.x | Current Version (1.0.1)
Defines a select list in the model class, to decide which columns to be selected from the result set. The select list is a series of expressions separated by commas. Multiple SqlSelectTable
attributes can be applied to one model class.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Inheritance Constructor
System.Attribute
Syntax
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = true)]
public class SqlSelectAttribute : Attribute, ISqlSelect
Constructors
Name | Description |
---|---|
SqlSelectAttribute(string selectName) | Initializes a new instance of the SqlSelectAttribute class. |
Properties
Name | Return Type | Description |
---|---|---|
RawSelect | string | Gets or sets the raw SQL select list that decides which columns to be selected from the result set. |
SelectName | string | Gets the name of the select list. |
Remarks
After a model class is mapped to the database table, we may only need to use some columns in the table, and we do not want to create multiple model classes for the same table; in such case, we can achieve this by applying one or more SqlSelect
attributes to the model class.
Examples
The following code example demonstrates how to use the SqlSelect attribute. It applies multiple SqlSelect attributes to the DepartmentSqlSelect class. Each of them represents an independent select list for generating different SQL SELECT statement.
Model: DepartmentSqlSelect
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")]
[SqlSelect("sqlselect", RawSelect = "DepartmentID, Name")]
[SqlSelect("sqlselect2", RawSelect = "DepartmentID, Name, Budget, StartDate")]
[SqlSelect("sqlselectname", RawSelect = "Name")]
public class DepartmentSqlSelect
{
[Key]
public Int32 DepartmentID { get; set; }
public String Name { get; set; }
public Decimal Budget { get; set; }
public DateTime StartDate { get; set; }
public Int32? Administrator { get; set; }
}
}
Example Method:
using Appeon.ApiDoc.Models.School;
using SnapObjects.Data;
using System;
namespace Appeon.ApiDoc.SqlSelectAttributeExamples
{
public class SqlSelectAttributeExample
{
private SchoolContext _context;
public SqlSelectAttributeExample(SchoolContext dataContext)
{
// Sets the data context.
_context = dataContext;
}
public void Example()
{
// Gets the SQL statement using the Select list specified by sqlselect.
string sql1 = ModelSqlBuilder.GetBuilder<DepartmentSqlSelect>(_context)
.GetQueryBuilder("sqlselect")
.ToSqlString(_context);
Console.WriteLine("The SQL expression generated by sqlselect is:");
Console.WriteLine(sql1);
// Executes the SQL statement 1.
var result1 = _context.SqlExecutor.Select<DepartmentSqlSelect>(sql1);
// Gets the SQL statement using the Select list specified by sqlselect2.
string sql2 = ModelSqlBuilder.GetBuilder<DepartmentSqlSelect>(_context)
.GetQueryBuilder("sqlselect2")
.ToSqlString(_context);
// Executes the SQL statement 2.
var result2 = _context.SqlExecutor.Select<DepartmentSqlSelect>(sql2);
Console.WriteLine();
Console.WriteLine("The SQL expression generated by sqlselect2 is:");
Console.WriteLine(sql2);
/* This code example produces the following output:
The SQL expression generated by sqlselect is:
SELECT
[DepartmentID],
[Name]
FROM[dbo].[Department]
The SQL expression generated by sqlselect2 is:
SELECT
[DepartmentID],
[Name],
[Budget],
[StartDate]
FROM[dbo].[Department]
*/
}
}
}
Applies to
.NET Standard
2.x