0% found this document useful (0 votes)
422 views

Entity Spaces Quick Reference

This document provides a quick reference guide for using EntitySpaces to work with data in a database. It outlines the requirements and steps to set up the demo, including preparing the code generation and custom classes. It then explains how to connect to data sources and perform common tasks like CRUD operations on entities and collections, dynamic queries, aggregates, grouping, filtering and more. It also touches on data binding and using custom classes.

Uploaded by

adikusdianto
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
422 views

Entity Spaces Quick Reference

This document provides a quick reference guide for using EntitySpaces to work with data in a database. It outlines the requirements and steps to set up the demo, including preparing the code generation and custom classes. It then explains how to connect to data sources and perform common tasks like CRUD operations on entities and collections, dynamic queries, aggregates, grouping, filtering and more. It also touches on data binding and using custom classes.

Uploaded by

adikusdianto
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

EntitySpaces Quick Reference Guide v1.

0
EntitySpaces Quick Reference Guide v1.0.......................................................................................................... 1
Requirements ............................................................................................................................................ 3
Running the Demo...................................................................................................................................... 3
Preparation................................................................................................................................................ 4
1. Copy Assemblies ............................................................................................................................. 4
2. Create Folders for Generated And Custom Code .................................................................................. 4
3. MyGeneration Setup ........................................................................................................................ 4
4. Execute Generated Classes Master Template ...................................................................................... 4
5. Execute Custom Classes Master Template .......................................................................................... 5
6. Execute Stored Procedure Templates ................................................................................................. 6
Visual Studio Setup .................................................................................................................................... 8
1. Add Config ..................................................................................................................................... 8
2. Add Generated Code Folders............................................................................................................. 8
3. Reference Assemblies ...................................................................................................................... 8
4. Include the Namespace .................................................................................................................... 8
Setting Connections.................................................................................................................................... 9
Connection Name ................................................................................................................................... 9
SQL Access Type .................................................................................................................................... 9
Entity Usage .............................................................................................................................................10
Create..................................................................................................................................................10
Read ....................................................................................................................................................10
Update .................................................................................................................................................10
Delete ..................................................................................................................................................10
Collection Usage........................................................................................................................................11
Read ....................................................................................................................................................11
Count...................................................................................................................................................11
Iteration ...............................................................................................................................................11
Find by Primary Key ...............................................................................................................................11
Sort .....................................................................................................................................................11
Filter ....................................................................................................................................................11
To Clear Sorting or Filtering ....................................................................................................................11
Dynamic Query Usage................................................................................................................................12
Enumerations........................................................................................................................................12
Basic Select (retrieves all rows and columns) ............................................................................................12
Default Conjunction - And.......................................................................................................................12
Or Conjunction ......................................................................................................................................12
Mixing And/Or Conjunctions ....................................................................................................................13
Aggregates Usage (Avg, Count, Min, Max, Sum, StdDev, and Var)...................................................................14
Count...................................................................................................................................................14
Sum.....................................................................................................................................................14
Aggregate With Distinct..........................................................................................................................14
Aggregate With Count ............................................................................................................................14
Aggregate With Where Clause .................................................................................................................14
Group By Usage ........................................................................................................................................15
Basic Group By......................................................................................................................................15
Multiple Group By’s and Where Clause......................................................................................................15
Group By With Select, Where, and Order By..............................................................................................15
Operator Usage.........................................................................................................................................16
Assignment...........................................................................................................................................16
Comparison ..........................................................................................................................................16
Distinct and Top Usage ..............................................................................................................................16
Distinct ................................................................................................................................................16
Top ......................................................................................................................................................16
Simplifying Queries....................................................................................................................................17
Query Caching.......................................................................................................................................17
Custom Loads .......................................................................................................................................17
Load Stored Procedure ...........................................................................................................................17
LastQuery – (Debugging Queries) ............................................................................................................17
Data Binding.............................................................................................................................................18
Design Time Grid Binding........................................................................................................................18

1
Combo Box/Drop Down List Binding .........................................................................................................18
Single Entity Binding ..............................................................................................................................18
Custom Classes.........................................................................................................................................19
Special Functions ......................................................................................................................................20
Text Mode: ...........................................................................................................................................20
ExecuteNonExec (See ExecuteNonQuery in the .NET documentation) .......................................................20
ExecuteReader ..................................................................................................................................20
ExecuteScalar....................................................................................................................................21
FillDataSet ........................................................................................................................................21
FillDataTable .....................................................................................................................................21
Stored Procedure Mode: .........................................................................................................................22
ExecuteNonExec ................................................................................................................................22
ExecuteReader ..................................................................................................................................22
ExecuteScalar....................................................................................................................................22
FillDataSet ........................................................................................................................................22
FillDataTable .....................................................................................................................................22
Serialization..............................................................................................................................................23
Transactions .............................................................................................................................................23
esTransactionScope ...............................................................................................................................23
Using Multiple Databases............................................................................................................................24
Switching Connections............................................................................................................................25

2
Requirements
• Microsoft .NET 2.0 Framework
• MyGeneration 1.1.5 (requires Microsoft .NET 1.1 Framework)
• EntitySpaces architecture for .NET (This guide assumes it is installed in the default location.)

Running the Demo


• Open the EntitySpaces SqlDemo solution located at C:\Program Files\EntitySpaces\SqlDemo\SqlDemo.sln.
• Modify the connectionString in the app.config file in the demo project to something relevant for your environment.
• Rebuild the Solution and Run.

app.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>

<configSections>
<sectionGroup name="EntitySpaces"
type="EntitySpaces.Interfaces.esConfigSettings, EntitySpaces.Core" >
<section name="connectionInfo"
type="EntitySpaces.Interfaces.esConfigSettings, EntitySpaces.Interfaces"
allowLocation="true" allowDefinition="Everywhere"
restartOnExternalChanges="true" />
</sectionGroup>
</configSections>

<EntitySpaces>
<connectionInfo default="SQL">
<connections>

<add name="SQL"
providerMetadataKey="esDefault"
sqlAccessType="DynamicSQL"
provider="EntitySpaces.SqlClientProvider"
providerClass="DataProvider"
connectionString="User ID=sa;Password=;
Initial Catalog=Northwind;Data Source=localhost"
databaseVersion="2005" />

</connections>
</connectionInfo>
</EntitySpaces>
</configuration>

3
Preparation

1. Copy Assemblies – The default location for the EntitySpaces assemblies is C:\Program
Files\EntitySpaces\SqlDemo\SqlDemo\Runtime\. You will need to copy EntitySpaces.Core.dll,
EntitySpaces.Interfaces.dll, and the appropriate assembly for your data provider
(EntitySpaces.SqlClientProvider.dll, EntitySpaces.MSAccessProvider.dll, EntitySpaces.MySqlClientProvider.dll, or
EntitySpaces.OracleClientProvider.dll) to your project bin folder.

2. Create Folders for Generated And Custom Code - Create two folders in your project, one
called Generated, and one called Custom. For ASP.NET 2.0 web applications putting these in your App_Code folder
is recommended. The custom classes need only be generated once, and this is where any custom business logic
you may have will be placed. The generated classes can be regenerated any time your database schema changes
and doing so will not touch your custom logic in anyway. This makes adjusting to schema changes very easy.

3. MyGeneration Setup - Open up MyGeneration Default Settings and ensure that Connection String is
pointing to the database of interest. The Language Mapping is hard coded into the templates, so it does not
matter what you select here. The Database Target Mapping settings are not necessary for the EntitySpaces
templates. The example below is for Microsoft SQL Server.

4. Execute Generated Classes Master Template - While still in MyGeneration, open the template
browser (2nd Image from the left on the toolbar), expand the EntitySpaces folder, then the C# folder, and right
click on the EntitySpaces Generated Classes Master Template and select Execute. You will be presented with the
following form. Ensure your output path for the generated object classes is what it should be. For the generated

4
classes it should be the Generated folder. Leave Generate a Single File checked. Leave Strongly Typed
DynamicQuery and Metadata Class Should Ignore Schema and Catalog unchecked. If you plan on accessing this
object on multiple database platforms you will also need to check Target Multiple Databases. If you have no need
for this, leave it unchecked. Repeat this step if needed for any views you may have.

5. Execute Custom Classes Master Template - Now right click on the EntitySpaces Custom Master
Template and select Execute. You will get a similar form to the previous one we noted for the generated class
template. Once again ensure your output path for the generated object classes is what it should be. For the
custom classes it should be the Custom folder. Repeat this step if needed for any views you may have.

5
6. Execute Stored Procedure Templates – Use this template only if your database platform
supports them. While still in the EntitySpaces template folder in MyGeneration expand the stored procedure
folder. Right click on the stored procedure template for the platform(s) you will be using and select Execute. You
will be presented with the form seen below. Perform the same operations stated above and check the box if you
will be using SQL Server 2005 or SQL Express. This will generate all the SQL scripts you need to create your
stored procedures for your generated objects. These scripts will need to be executed in your database
administration application.

6
7
Visual Studio Setup

1. Add Config Entries - Add the necessary EntitySpaces configuration settings to your applications Web or
App .config file. See here for the example again.

2. Add Generated Code Folders - If using ASP.NET 2.0 and the new code beside compilation model
your object classes should be automatically detected in the App_Code folder as well as the assemblies we
copied to the bin folder, if using the web project structure or a windows form application you will need to add
the newly generated object code by right clicking on the project file and selecting Add -> Existing Item. You
will need to do this for all generated classes in both the Generated and Custom folders.

3. Reference Assemblies - Add a reference to EntitySpaces.Core.dll, EntitySpaces.Interfaces.dll, and


the appropriate assembly for your data provider by right clicking on references in the solution explorer and
selecting the required EntitySpaces assemblies.

4. Include the Namespace - Ensure that you add your using statement for the BusinessObjects
namespace (unless you used something different when generating your objects. If that is the case, substitute
your namespace here).

8
Setting Connections
When you new an entity or collection, it will take the settings from your default as defined in app.config. Normally
these do not need to be changed in code unless you need to connect to multiple databases. See Using Multiple
Databases for more details.

• providerMetadataKey is esDefault for classes generated from the master templates. Use the same name
you used in the MetadataMap template for additional connections.
• sqlAccessType can be either DynamicSql or StoredProcedure. This affects loads and saves.
• provider is one of the providers included in EntitySpaces.
• providerClass affects transactions. Use DataProvider for esTransactionScope. Use
DataProviderEnterprise for the new .NET 2 TransactionScope Class for databases that support it.
• connectionString is the string necessary to connect to your database.
• databaseVersion is optional. For Sql Server, 2005 tells EntitySpaces how to deal with concurrency.

Connection Name
Corresponds to the name key in app.config

Employees entity = new Employees();

entity.es.Connection.Name = "SQL";
entity.AddNew();
entity.FirstName = this.txtFirstName.Text;
entity.LastName = this.txtLastName.Text;
entity.Save();

Employees entity = new Employees();

entity.es.Connection.Name = "ACCESS";
entity.AddNew();
entity.FirstName = this.txtFirstName.Text;
entity.LastName = this.txtLastName.Text;
entity.Save();

SQL Access Type


Stored Procedure or Dynamic SQL

Employees entity = new Employees();

entity.es.Connection.SqlAccessType = esSqlAccessType.StoredProcedure;
entity.AddNew();
entity.FirstName = this.txtFirstName.Text;
entity.LastName = this.txtLastName.Text;
entity.Save();

Employees entity = new Employees();

Entity.es.Connection.SqlAccessType = esSqlAccessType.DynamicSQL;
entity.AddNew();
entity.FirstName = this.txtFirstName.Text;
entity.LastName = this.txtLastName.Text;
entity.Save();

9
Entity Usage
Create
Employees entity = new Employees();

entity.AddNew();
entity.FirstName = this.txtFirstName.Text;
entity.LastName = this.txtLastName.Text;
entity.Save();

Read
Employees entity = new Employees();

entity.LoadByPrimaryKey(101);

Update
Employees entity = new Employees();

entity.LoadByPrimaryKey(EmployeeId);
entity.FirstName = this.txtFirstName.Text;
entity.LastName = this.txtLastName.Text;
entity.Save();

Delete
Employees entity = new Employees();

entity.LoadByPrimaryKey(employeeId);
entity.MarkAsDeleted();
entity.Save();

10
Collection Usage
Read
EmployeesCollection collection = new EmployeesCollection();

collection.LoadAll();

Count
EmployeesCollection collection = new EmployeesCollection();

collection.LoadAll();
int TotalEntities = collection.Count;

Iteration
EmployeesCollection collection = new EmployeesCollection();

collection.LoadAll();
foreach (Employees entity in collection)
{
Console.WriteLine(entity.LastName);
}

Find by Primary Key


EmployeesCollection collection = new EmployeesCollection();
Employees entity = collection.FindByPrimaryKey(1);

Console.WriteLine(entity.FirstName);

Sort
EmployeesCollection collection = new EmployeesCollection();

collection.LoadAll();
collection.Sort = "LastName ASC";

Filter
EmployeesCollection collection = new EmployeesCollection();

collection.LoadAll();
collection.Filter = "LastName = 'Jones'";

To Clear Sorting or Filtering


collection.Filter = "";
collection.Sort = "";

11
Dynamic Query Usage
(Additional examples can be found here.)

Enumerations
1) Conjunctions
a) esConjunction.And
b) esConjunction.Or

2) Direction
a) esOrderByDirection.Ascending
b) esOrderByDirection.Descending

3) Operands
a) esWhereOperand.Equal
b) esWhereOperand.NotEqual
c) esWhereOperand.GreaterThan
d) esWhereOperand.GreaterThanOrEqual
e) esWhereOperand.LessThan
f) esWhereOperand.LessThanOrEqual
g) esWhereOperand.Like
h) esWhereOperand.NotLike
i) esWhereOperand.IsNull
j) esWhereOperand.IsNotNull
k) esWhereOperand.Between
l) esWhereOperand.In
m) esWhereOperand.NotIn

Basic Select (retrieves all rows and columns)


EmployeesCollection collection = new EmployeesCollection();

collection.Query.Load();

Default Conjunction - And


EmployeesCollection collection = new EmployeesCollection();

collection.Query.Where(Collection.Query.LastName.Like("A%"),
collection.Query.FirstName.Like("A%"));
collection.Query.Load();

Or Conjunction
EmployeesCollection collection = new EmployeesCollection();

collection.Query.es.DefaultConjunction = esConjunction.Or;
collection.Query.Where(collection.Query.LastName.Like("A%"),
collection.Query.FirstName.Like("A%"));
Collection.Query.Load();

12
Mixing And/Or Conjunctions
EmployeesCollection collection = new EmployeesCollection();

collection.Query
.Select
(
collection.Query.EmployeeID,
collection.Query.FirstName,
collection.Query.LastName
)
.Where
(
collection.Query.Or
(
collection.Query.LastName.Like("%A%"),
collection.Query.LastName.Like("%O%")
),
collection.Query.BirthDate.Between("1/1/1940", "1/1/2006")
)
.OrderBy
(
collection.Query.LastName.Descending,
collection.Query.FirstName.Ascending
);
collection.Query.Load();

13
Aggregates Usage (Avg, Count, Min, Max, Sum, StdDev, and Var)
Count
EmployeesCollection collection = new EmployeesCollection();

collection.Query.Select
(
collection.Query.Salary.Count("SalariedEmployees")
);
collection.Query.Load();

Sum
EmployeesCollection collection = new EmployeesCollection();

collection.Query.Select
(
collection.Query.Salary.Sum("TotalSaleries")
);
collection.Query.Load();

Aggregate With Distinct


EmployeesCollection collection = new EmployeesCollection();

collection.Query.Select
(
collection.Query.Salary.Sum("TotalSaleries", true)
);
collection.Query.Load();

Aggregate With Count


EmployeesCollection collection = new EmployeesCollection();

collection.Query.Select
(
collection.Query.Salary.Sum("TotalSaleries", true)
);
collection.Query.es.CountAll = true;
collection.Query.es.CountAllAlias = "TotalCount";
collection.Query.Load();

Aggregate With Where Clause


EmployeesCollection collection = new EmployeesCollection();

collection.Query.es.CountAll = true;
collection.Query.es.CountAllAlias = "Total";
collection.Query.Where
(
collection.Query.IsActive.Equal(true)
);
collection.Query.Load();

14
Group By Usage
Basic Group By
EmployeesCollection collection = new EmployeesCollection();

collection.Query.es.CountAll = true;
collection.Query.es.CountAllAlias = "TotalEmployees";
collection.Query
.Select (collection.Query.IsActive)
.GroupBy(collection.Query.IsActive);
collection.Query.Load();

Multiple Group By’s and Where Clause


EmployeesCollection collection = new EmployeesCollection();

collection.Query.es.CountAll = true;
collection.Query
.Select
(
collection.Query.IsActive,
collection.Query.DepartmentID
)
.Where
(
collection.Query.IsActive.Equal(true)
)

.GroupBy
(
collection.Query.IsActive,
collection.Query.DepartmentID
);
collection.Query.Load();

Group By With Select, Where, and Order By


EmployeesCollection collection = new EmployeesCollection();

collection.Query.es.CountAll = true;
collection.Query
.Select (collection.Query.IsActive,
collection.Query.DepartmentID)
.Where (collection.Query.IsActive.Equal(true))
.GroupBy(collection.Query.IsActive,
collection.Query.DepartmentID)
.OrderBy(collection.Query.DepartmentID.Ascending,
collection.Query.IsActive.Ascending);
collection.Query.Load();

15
Operator Usage
Assignment
EmployeesCollection collection = new EmployeesCollection();

collection.Query.Where(collection.Query.IsActive == true);
collection.Query.Load();

Comparison
EmployeesCollection collection = new EmployeesCollection();

collection.Query.Where(collection.Query.Salary >= 30.00);


collection.Query.Load();

Distinct and Top Usage


Distinct
EmployeesCollection collection = new EmployeesCollection();

collection.Query.Select
(
collection.Query.LastName,
collection.Query.FirstName
);
collection.Query.es.Distinct = true;
collection.Query.Load();

Top
EmployeesCollection collection = new EmployeesCollection();

collection.Query.OrderBy(collection.Query.Salary.Descending);
collection.Query.es.Top = 5;
collection.Query.Load();

16
Simplifying Queries
Query Caching
EmployeesCollection collection = new EmployeesCollection();

EmployeesQuery q = collection.Query;
q.Select(q.EmployeeID, q.FirstName, q.LastName)
.Where
(
q.Or(q.LastName.Like("%A%"), q.LastName.Like("%O%")),
q.BirthDate.Between("1/1/1940", "1/1/2006")
)
.OrderBy(q.LastName.Descending, q.FirstName.Ascending);
q.Load();

Custom Loads
public partial class EmployeesQuery: esEmployeesQuery
{
public bool CustomLoad()
{
Select(EmployeeID, FirstName, LastName, TitleOfCourtesy)
.Where
(
Or(LastName.Like("%A%"), LastName.Like("%O%")),
this.BirthDate.Between("1/1/1940", "1/1/2006")
)
.OrderBy(LastName.Descending, FirstName.Ascending);
return this.Load();
}
}

Load Stored Procedure


public bool LoadByEmployeeType(string employeeTypeId)
{
esParameters parms = new esParameters();
parms.Add("EmployeeTypeId", employeeTypeId)
return this.Load(esQueryType.StoredProcedure, "proc_LoadEmployeesByType", parms);
}

LastQuery – (Debugging Queries)


Employees entity = new Employees();

entity.Query.es.DefaultConjunction = esConjunction.Or;
entity.Query.Where(entity.Query.LastName.Like("A%"), entity.Query.FirstName.Like("A%"));
entity.Query.Load();

string SqlText = entity.Query.es.LastQuery;

17
Data Binding
Design Time Grid Binding
this.employeesCollection1 = new EmployeesCollection();
this.employeesCollection1.Query.OrderBy(
this.employeesCollection1.Query.LastName.Ascending);
this.employeesCollection1.Query.Load();

this.dgvEmployees.DataSource = null;
this.dgvEmployees.DataSource = this.employeesCollection1;
this.dgvEmployees.Refresh();

Combo Box/Drop Down List Binding


EmployeesCollection collection = new EmployeesCollection();

collection.Query.OrderBy(collection.Query.DisplayName.Ascending);
collection.Query.Load();

this.cmbEmployee.DataSource = null;
this.cmbEmployee.DataSource = collection;
this.cmbEmployee.DisplayMember = EmployeesMetadata.ColumnNames.DisplayName;
this.cmbEmployee.ValueMember = EmployeesMetadata.ColumnNames.EmployeeId;

Single Entity Binding


Employees entity = new Employees();

entity.LoadByPrimaryKey(employeeId);
this.cmbEmployeeType.DataBindings.Add(new Binding("SelectedValue", entity,
EmployeesMetadata.PropertyNames.EmployeeTypeId, true,
DataSourceUpdateMode.OnPropertyChanged));
this.txtFirstName.DataBindings.Add(new Binding("Text", entity,
EmployeesMetadata.PropertyNames.FirstName, true,
DataSourceUpdateMode.OnPropertyChanged));
this.txtLastName.DataBindings.Add(new Binding("Text", entity,
EmployeesMetadata.PropertyNames.LastName, true,
DataSourceUpdateMode.OnPropertyChanged));
this.chkActive.DataBindings.Add(new Binding("Checked", entity,
EmployeesMetadata.PropertyNames.Active, true, DataSourceUpdateMode.OnPropertyChanged));

18
Custom Classes
It is no longer necessary to write your custom logic directly in the generated concrete classes. Because the concrete
classes are partial classes, you can add your custom methods in a separate file in a separate folder in your project.
The same technique can be used for Custom Queries and the methods in the Special Functions section.

EmployeesCollectionCustom.cs
namespace BusinessObjects
{
public partial class EmployeesCollection : esEmployeesCollection
{
public int CustomExecuteNonExec()
{
// Custom code
}
}
}

19
Special Functions
These methods can be used in text mode to pass raw SQL, stored procedure mode to call a stored procedure, or
command mode. See the .NET documentation for an explanation of these functions.

Text Mode:
ExecuteNonExec (See ExecuteNonQuery in the .NET documentation)
public partial class EmployeesCollection : esEmployeesCollection
{
public int CustomExecuteNonExecText(string newName)
{
string sqlText = "";
esParameters esParams = new esParameters();
esParams.Add("FirstName", newName);
esParams.Add("LastName", "Doe");
esParams.Add("Salary", 27.53);

sqlText = "UPDATE [Employees] ";


sqlText += "SET [FirstName] = @FirstName ";
sqlText += "WHERE [LastName] = @LastName ";
sqlText += "AND [Salary] = @Salary";

return this.ExecuteNonExec(esQueryType.Text, sqlText, esParams);


}
}

ExecuteReader
public partial class EmployeesCollection : esEmployeesCollection
{
public IDataReader CustomReaderText()
{
string sqlText = "";
esParameters esParams = new esParameters();
esParams.Add("LastName", "Doe");

sqlText = "SELECT * ";


sqlText += "FROM [Employees] ";
sqlText += "WHERE [LastName] = @LastName";

return this.ExecuteReader(esQueryType.Text, sqlText, esParams);


}
}

20
ExecuteScalar
public partial class EmployeesCollection : esEmployeesCollection
{
public int CustomExecuteScalarText()
{
string sqlText = "";
esParameters esParams = new esParameters();
esParams.Add("LastName", "Doe");

sqlText = "SELECT COUNT([FirstName]) ";


sqlText += "FROM [Employees] ";
sqlText += "WHERE [LastName] = @LastName";

return Convert.ToInt32(this.ExecuteScalar(esQueryType.Text,
sqlText, esParams));
}
}

FillDataSet
public partial class EmployeesCollection : esEmployeesCollection
{
public DataSet CustomFillDataSetText()
{
string sqlText = "";

sqlText = "SELECT * ";


sqlText += "FROM [Employees] ";
sqlText += "WHERE [LastName] = {0}";

return this.FillDataSet(esQueryType.Text, sqlText, "Doe");


}
}

FillDataTable
public partial class EmployeesCollection : esEmployeesCollection
{
public DataTable CustomFillDataTableText()
{
string sqlText = "";

sqlText = "SELECT * ";


sqlText += "FROM [Employees] ";
sqlText += "WHERE [LastName] = {0} ";
sqlText += "OR [LastName] = {1}";

return this.FillDataTable(esQueryType.Text, sqlText, "Doe", "Johnson");


}
}

21
Stored Procedure Mode:
ExecuteNonExec
public partial class EmployeesCollection : esEmployeesCollection
{
public int CustomExecuteNonExec()
{
return this.ExecuteNonExec(this.es.DatabaseSchema, this.es.spLoadAll);
}
}

ExecuteReader
public partial class EmployeesCollection : esEmployeesCollection
{
public IDataReader CustomReader()
{
return this.ExecuteReader(this.es.DatabaseSchema, this.es.spLoadAll);
}
}

ExecuteScalar
public partial class EmployeesCollection : esEmployeesCollection
{
public string CustomExecuteScalar()
{
return this.ExecuteScalar(this.es.DatabaseSchema,
this.es.spLoadAll).ToString();
}
}

FillDataSet
public partial class EmployeesCollection : esEmployeesCollection
{
public DataSet CustomFillDataSet()
{
DataSet dataSet = this.FillDataSet(this.es.DatabaseSchema,
this.es.spLoadAll);
return dataSet;
}
}

FillDataTable
public partial class EmployeesCollection : esEmployeesCollection
{
public DataTable CustomFillDataTable()
{
DataTable dataTable = this.FillDataTable(this.es.DatabaseSchema,
this.es.spLoadAll);
return dataTable;
}
}

22
Serialization
EmployeesCollection collection = new EmployeesCollection();

BinaryFormatter bf = new BinaryFormatter();


MemoryStream ms = new MemoryStream();
bf.Serialize(ms, collection);

EmployeesCollection collectionClone = new EmployeesCollection();

ms.Position = 0;
collectionClone = (EmployeesCollection)bf.Deserialize(ms);
ms.Close();

Transactions
You do not need to wrap saves on a single collection in a transaction. EntitySpaces does this for you, even if you have
multiple inserts, updates, and deletes. Use transactions if you are saving two or more objects that need to rollback as
a set in the event of a failure. EntitySpaces has two transaction models. esTransactionScope works with all supported
databases and is similar to dOOdads transactions. Set providerClass="DataProvider" in your app.config.
TransactionScope uses the new .NET 2.0 TransactionScope class for databases that support it. Set
providerClass="DataProviderEnterprise" in your app.config.

esTransactionScope
OrdersCollection ordersCollection = new OrdersCollection();
Orders ordersEntity = new Orders();
OrderDetailsCollection orderDetailsCollection = new OrderDetailsCollection();
OrderDetails orderDetailsEntity = new OrderDetails();

ordersEntity = ordersCollection.AddNew();
int orderId = ordersEntity.Id.Value;
ordersEntity.str.CustomerId = "3";

orderDetailsEntity = orderDetailsCollection.AddNew();
orderDetailsEntity.OrderId = orderId;
orderDetailsEntity.str.ProductId = "147";
orderDetailsEntity = orderDetailsCollection.AddNew();
orderDetailsEntity.OrderId = orderId;
orderDetailsEntity.str.ProductId = "255";

using(esTransactionScope scope = new esTransactionScope())


{
ordersCollection.Save();
orderDetailsCollection.Save();
scope.Complete();
}

23
Using Multiple Databases
EntitySpaces is provider independent and can dynamically switch between databases at runtime. The databases must
have matching schema and you need to configure each database in app.config. For one database run both the master
templates. For the second database, run the MetadataMap template (see image below.) The mapName for the master
templates is esDefault and, as in the example config file above, you should set providerMetadataKey="esDefault"
for your default config entry, in this case "SQL". For the MetadataMap template, give it a unique mapName; i.e.,
esAccess, and use that in the providerMetadatKey for the second database config, in this case "ACCESS". In your
code, every time you new an object it will have your default settings. If you need to access the second (or
subsequent) configs, you should set the connectionName.

24
Switching Connections
// This uses your default config; e.g., Sql Server ("SQL")
OrdersCollection collection = new OrdersCollection();
collection.LoadAll();

// This uses your alternate config; e.g., Access ("ACCESS")


OrdersCollection collection = new OrdersCollection();
collection.es.Connection.Name = "ACCESS";
collection.LoadAll();

Copyright © 2005 – 2006, EntitySpaces, LLC

25

You might also like