IDataStoreBase.DataContext Property
.NET Standard 2.x
Gets or sets the entry to the database associated with DataStore.
Namespace: DWNet.Data
Assembly: DWNet.Data.dll
Syntax
DataContext DataContext { get; set; }
Property Value
SnapObjects.Data.DataContext
The entry to the database associated with DataStore.
Examples
The following code example demonstrates how to use the DataContext
property.
using Appeon.ApiDoc.Models;
using DWNet.Data;
using SnapObjects.Data;
using System;
using System.Collections.Generic;
namespace Appeon.ApiDoc.IDataStoreBaseExamples
{
public class DataContextExample
{
private readonly SchoolContext _context;
public DataContextExample(SchoolContext dataContext)
{
// Sets the data context
_context = dataContext;
}
public void Example()
{
// Instantiates datastore with datawindow: d_department.
var datastore = new DataStore("d_department", _context);
var context = datastore.DataContext;
// Gets the database connection information.
Console.WriteLine("ConnectionId: {0}", context.CurrentConnection.ConnectionId);
Console.WriteLine("CommandTimeout: {0}", context.ContextOptions.CommandTimeout);
Console.WriteLine("ConnectionString: {0}", context.CurrentConnection.ConnectionString);
// Gets data using SQL.
string sql = ModelSqlBuilder.GetBuilder<D_Department>(context).QuerySql;
var partments = context.SqlExecutor.Select<D_Department>(sql);
Console.WriteLine("D_Department Count: {0} ", partments.Count);
// Creates an instance.
var department = new D_Department
{
Departmentid = 10,
Name = "Design",
Budget = 3000,
Startdate = DateTime.Now,
Administrator = 1
};
// Adds a new instance to database.
int InsertedCount = context.SqlModelMapper.TrackCreate(department).SaveChanges().InsertedCount;
Console.WriteLine("InsertedCount: {0}", InsertedCount);
/*This code produces the following output:
ConnectionId: {da066fd7-1699-4d31-9668-77bd265da46b}
CommandTimeout: 30
ConnectionString: Data Source=***,1433;Initial Catalog=***...
D_Department Count: 4
InsertedCount: 1
*/
}
}
}
Example Refer To
Model Class: D_Department
DataWindow File: d_department