SqlColumnAttribute Class
.NET Standard 2.x
Represents the database column that a property is mapped to.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Inheritance Constructor
System.Attribute
Syntax
[AttributeUsage(AttributeTargets.Property)]
public class SqlColumnAttribute : Attribute, ISqlColumn
Constructors
Name | Description |
---|---|
SqlColumnAttribute(string column) | Initializes a new instance of the SqlColumnAttribute class. |
SqlColumnAttribute(string tableAlias, string column) | Initializes a new instance of the SqlColumnAttribute class. |
SqlColumnAttribute(string tableAlias, string column, string columnAlias) | Initializes a new instance of the SqlColumnAttribute class. |
Properties
Name | Return Type | Description |
---|---|---|
Column | string | Gets the name of the column the property is mapped to. |
ColumnAlias | string | Gets the alias of the column the property is mapped to. |
ColumnAliasDefined | bool | Gets whether the column has an alias. |
Order | int | Gets or sets the zero-based order of the column the property is mapped to. |
TableAlias | string | Gets the alias of the table where the column belongs to. |
TableAliasDefined | bool | Gets whether the table (where the column belongs to) has an alias. |
TypeName | string | Gets or sets the data type (specific to the database provider) of the column the property is mapped to. |
Remarks
Compared to the System.ComponentModel.DataAnnotations.Schema.ColumnAttribute
class provided by .NET, the only difference is we support the column alias and the table alias.
If there is only one table in the model class that needs to be mapped, and the property name of the model class is the same as the column name of the database table (case is insensitive), then the SqlColumn
attribute can be ignored.
Examples
Example 1
The following code example demonstrates how to use the constructor: SqlColumn (columnname). When a property in the model is applied with SqlColumn (columnname), it indicates the property is mapped to that table column.
Model: DepartmentSqlColumn
using SnapObjects.Data;
using System;
using System.ComponentModel.DataAnnotations;
namespace Appeon.ApiDoc.Models.School
{
[FromTable("Department", Schema = "dbo")]
public class DepartmentSqlColumn
{
[Key]
public int DepartmentID { get; set; }
[SqlColumn("Name")]
public string DepartmentName { get; set; }
public decimal Budget { get; set; }
public DateTime StartDate { get; set; }
public int? Administrator { get; set; }
}
}
Example Method: Example1
using SnapObjects.Data;
using Appeon.ApiDoc.Models.School;
using System;
namespace Appeon.ApiDoc.SqlColumnAttributeExamples
{
public class SqlColumnAttributeExample
{
private SchoolContext _context;
public SqlColumnAttributeExample(SchoolContext dataContext)
{
// Sets the data context.
_context = dataContext;
}
public void Example1()
{
// Gets the SQL statement.
string sql = ModelSqlBuilder.GetBuilder<DepartmentSqlColumn>(_context).QuerySql;
Console.WriteLine("SQL statement:");
Console.WriteLine(sql);
// Executes the SQL statement.
var departments = _context.SqlExecutor.Select<DepartmentSqlColumn>(sql);
/* The code produces the following output:
SQL statement:
SELECT
[DepartmentID],
[Name],
[Budget],
[StartDate],
[Administrator]
FROM [dbo].[Department]
*/
}
}
}
Example 2
The following code example demonstrates how to use the constructor: SqlColumn (tableAlias, columnname). When a property in the model is applied with SqlColumn (tableAlias, columnname), it indicates the property is mapped to the specified column in the specified table. tableAlias is defined in Table or FromTable.
Model: DepartmentSqlColumn2
using SnapObjects.Data;
using System;
using System.ComponentModel.DataAnnotations;
namespace Appeon.ApiDoc.Models.School
{
[FromTable("depart", "Department", Schema = "dbo")]
public class DepartmentSqlColumn2
{
[Key]
public int DepartmentID { get; set; }
[SqlColumn("depart", "Name")]
public string DepartmentName { get; set; }
public decimal Budget { get; set; }
public DateTime StartDate { get; set; }
public int? Administrator { get; set; }
}
}
Example Method: Example2
public void Example2()
{
// Gets the SQL statement.
string sql = ModelSqlBuilder.GetBuilder<DepartmentSqlColumn2>(_context).QuerySql;
Console.WriteLine("SQL statement:");
Console.WriteLine(sql);
// Executes the SQL statement.
var departments = _context.SqlExecutor.Select<DepartmentSqlColumn2>(sql);
/* The code produces the following output:
SQL statement:
SELECT
[DepartmentID],
[depart].[Name],
[Budget],
[StartDate],
[Administrator]
FROM [dbo].[Department] AS [depart]
*/
}
Example 3
The following code example demonstrates how to use the constructor: SqlColumn (tableAlias, columnname, columnAlias). When a property in the model is applied with SqlColumn (tableAlias, columnname, columnAlias), it indicates the property is mapped to the specified column in the specified table. columnAlias is the alias name of column.
Model: DepartmentSqlColumn3
using SnapObjects.Data;
using System;
using System.ComponentModel.DataAnnotations;
namespace Appeon.ApiDoc.Models.School
{
[FromTable("depart", "Department", Schema = "dbo")]
public class DepartmentSqlColumn3
{
[Key]
public int DepartmentID { get; set; }
[SqlColumn("depart", "Name", "DepartName")]
public string DepartName { get; set; }
public string Name { get; set; }
public decimal Budget { get; set; }
public DateTime StartDate { get; set; }
public int? Administrator { get; set; }
}
}
Example Method: Example3
public void Example3()
{
// Gets the SQL statement.
string sql = ModelSqlBuilder.GetBuilder<DepartmentSqlColumn3>(_context).QuerySql;
Console.WriteLine("SQL statement:");
Console.WriteLine(sql);
// Executes the SQL statement.
var departments = _context.SqlExecutor.Select<DepartmentSqlColumn3>(sql);
/* The code produces the following output:
SQL statement:
SELECT
[DepartmentID],
[depart].[Name],
[Name],
[Budget],
[StartDate],
[Administrator]
FROM [dbo].[Department] AS [depart]
*/
}
Applies to
.NET Standard
2.x