SqlComputeAttribute Class
.NET Standard 2.x
Represents a computed column that a property is mapped to.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Inheritance Constructor
System.Attribute
Syntax
[AttributeUsage(AttributeTargets.Property)]
public class SqlComputeAttribute : Attribute, ISqlCompute
Constructors
Name | Description |
---|---|
SqlComputeAttribute(string expression) | Initializes a new instance of the SqlComputeAttribute class. |
SqlComputeAttribute(string expression, string alias) | Initializes a new instance of the SqlComputeAttribute class. |
Properties
Name | Return Type | Description |
---|---|---|
Alias | string | Gets the alias of the computed column. |
AliasDefined | bool | Gets whether the computed column has an alias. |
Expression | string | Gets the SQL expression which represents the computed column. |
Order | int | Gets or sets the zero-based order of the computed column the property is mapped to. |
TypeName | string | Gets or sets the data type (specific to the database provider) of the computed column the property is mapped to. |
Remarks
When you define the computed column, the value is calculated by the DBMS when the data is retrieved.
Examples
Example 1
The following code example demonstrates how to use the constructor: SqlCompute (expression). When a property in the model is applied with SqlCompute (expression), it indicates the value of that property comes from the computed result of the expression.
Model: PersonSqlCompute
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using SnapObjects.Data;
namespace Appeon.ApiDoc.Models.School
{
[Table("Person", Schema = "dbo")]
public class PersonSqlCompute
{
[Key]
[Identity]
public int PersonID { get; set; }
[SqlCompute("LastName +'.' + FirstName")]
public string FullName { get; set; }
public DateTime? HireDate { get; set; }
public DateTime? EnrollmentDate { get; set; }
public string Discriminator { get; set; }
}
}
Example Method: Example1
using SnapObjects.Data;
using Appeon.ApiDoc.Models.School;
using System;
namespace Appeon.ApiDoc.SqlComputeAttributeExamples
{
public class SqlComputeAttributeExample
{
private SchoolContext _context;
public SqlComputeAttributeExample(SchoolContext dataContext)
{
// Sets the data context.
_context = dataContext;
}
public void Example1()
{
// Gets the SQL statement.
string sql = ModelSqlBuilder.GetBuilder<PersonSqlCompute>(_context).QuerySql;
Console.WriteLine("SQL statement:");
Console.WriteLine(sql);
// Executes the SQL statement.
var departments = _context.SqlExecutor.Select<PersonSqlCompute>(sql);
/* The code produces the following output:
SQL statement:
SELECT
[PersonID],
[LastName] +'.' + [FirstName],
[HireDate],
[EnrollmentDate],
[Discriminator]
FROM [dbo].[Person]
*/
}
}
}
Example 2
The following code example demonstrates how to use the constructor: SqlCompute (expression, alias). When a property in the model is applied with SqlCompute (expression, alias), it indicates the value of that property comes from the computed result of the expression. alias indicates the alias name of the computed field.
Model: PersonSqlCompute2
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using SnapObjects.Data;
namespace Appeon.ApiDoc.Models.School
{
[Table("Person", Schema = "dbo")]
public class PersonSqlCompute2
{
[Key]
[Identity]
public int PersonID { get; set; }
[SqlCompute("LastName +'.' + FirstName", "FullName")]
public string FullName { get; set; }
public DateTime? HireDate { get; set; }
public DateTime? EnrollmentDate { get; set; }
public string Discriminator { get; set; }
}
}
Example Method: Example2
public void Example2()
{
// Gets the SQL statement.
string sql = ModelSqlBuilder.GetBuilder<PersonSqlCompute2>(_context).QuerySql;
Console.WriteLine("SQL statement:");
Console.WriteLine(sql);
// Executes the SQL statement.
var department = _context.SqlExecutor.Select<PersonSqlCompute2>(sql);
/* The code produces the following output:
SQL statement:
SELECT
[PersonID],
[LastName] +'.' + [FirstName] AS [FullName],
[HireDate],
[EnrollmentDate],
[Discriminator]
FROM [dbo].[Person]
*/
}
Applies to
.NET Standard
2.x