FromTableAttribute Class
.NET Standard 2.x | Current Version (1.0.1)
Specifies the database table that a model class is mapped to. Multiple FromTable
attributes can be applied to one model class.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Inherited Constructor
System.ComponentModel.DataAnnotations.Schema.TableAttribute
Syntax
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = true)]
public class FromTableAttribute : TableAttribute, IFromTable
Constructors
Name | Description |
---|---|
FromTableAttribute(string name) | Initializes a new instance of the FromTableAttribute class. |
FromTableAttribute(string alias, string name) | Initializes a new instance of the FromTableAttribute class. |
Properties
Name | Return Type | Description |
---|---|---|
Alias | string | Gets the alias of the specified database table. |
AliasDefined | bool | Gets whether the specified database table has an alias or not. |
Name | String | Gets the name of the table. |
Schema | String | Gets or sets the schema of the table. |
WhereRaw | string | Gets or sets the raw WHERE clause for the specified database table. |
Remarks
The FromTable
attribute is equivalent to the database table specified in the FROM clause of the SQL SELECT statement. One or more FromTable
attributes can be applied to one model class in order to retrieve data from one or more database tables in one model.
When no Table
attribute is applied, and one or more FromTable
attributes are being applied to a model class, the first FromTable
attribute denotes the only one database table that the model class should use when executing the SQL DML.
Examples
Example 1
The following code example demonstrates how to use the constructor: FromTable (name, Propreties: Schema, WhereRaw) to apply the FromTable attribute to the PersonFromTable class.
Model: PersonFromTable
using SnapObjects.Data;
using System;
namespace Appeon.ApiDoc.Models.School
{
/// <summary>
/// This model class maps to the dbo.Person table.
/// </summary>
[FromTable("Person", Schema = "dbo", WhereRaw = "PersonID < 10")]
public class PersonFromTable
{
public Int32 PersonID { get; set; }
public String LastName { get; set; }
public String FirstName { 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.FromTableAttributeExamples
{
public class FromTableAttributeExample
{
private SchoolContext _context;
public FromTableAttributeExample(SchoolContext dataContext)
{
// Sets the data context.
_context = dataContext;
}
public void Example1()
{
// Gets the SQL statement.
string sql = ModelSqlBuilder.GetBuilder<PersonFromTable>(_context).QuerySql;
Console.WriteLine("SQL statement:");
Console.WriteLine(sql);
// Executes the SQL by a SqlExecutor object.
var persons = _context.SqlExecutor.Select<PersonFromTable>(sql);
/* This code example produces the following output:
SQL statement:
SELECT
[PersonID],
[LastName],
[FirstName],
[HireDate],
[EnrollmentDate],
[Discriminator]
FROM [dbo].[Person]
WHERE ([PersonID] < 10)
*/
}
}
}
Example 2
The following code example demonstrates how to use the constructor: FromTable (alias, name, Propreties: Schema, WhereRaw) to apply the FromTable attribute to the PersonFromTable2 class. It specfies the raw WHERE clause using the WhereRaw parameter.
Model: PersonFromTable2
using SnapObjects.Data;
using System;
namespace Appeon.ApiDoc.Models.School
{
/// <summary>
/// This model class maps to the dbo.Person table.
/// </summary>
[FromTable("p", "Person", Schema = "dbo", WhereRaw = "PersonID < 10")]
public class PersonFromTable2
{
public Int32 PersonID { get; set; }
[SqlColumn(tableAlias: "p", column: "LastName")]
public String PersonLastName { get; set; }
public String FirstName { 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<PersonFromTable2>(_context).QuerySql;
Console.WriteLine("SQL statement:");
Console.WriteLine(sql);
// Executes the SQL by a SqlExecutor object.
var persons = _context.SqlExecutor.Select<PersonFromTable2>(sql);
/* This code example produces the following output:
SQL statement:
SELECT
[PersonID],
[p].[LastName],
[FirstName],
[HireDate],
[EnrollmentDate],
[Discriminator]
FROM [dbo].[Person] AS [p]
WHERE ([PersonID] < 10)
*/
}
Applies to
.NET Standard
2.x