Step4:分别选择Business Entities Project、Data Access Project、Host Project。
添加数据库连接
Step1:接下来需要添加数据库连接
Step2:输入数据库连接串的名称:
创建连接完成后,将会在配置文件添加代码:
<connectionStrings>
<add name="RFConnectionString" connectionString="Data Source=Esint-lhj\Sql2005;Initial Catalog=AdventureWorksDW;Persist Security Info=True;User ID=sa;Password="
providerName="System.Data.SqlClient" />
</connectionStrings>
创建实体类
用Repository Factory可以很方便的通过数据库架构,生成业务实体的代码。
Step1:创建业务实体
Step2:选择数据库连接字符串
Step3:选择数据表、视图和字段
Step4:设置实体的属性了,可以设置业务实体的名称,默认的是表名;设置业务实体的属性名和该属性是否为只读属性
点击完成后,会生成业务实体的代码,并且为局部类型,这样便于用户在该业务实体上添加自己的一些操作,示例代码如下:
public partial class DimGeography
{
public DimGeography()
{
}
public DimGeography(System.String city, System.String countryRegionCode, System.String englishCountryRegionName,
System.String frenchCountryRegionName, System.Int32 geographyKey, System.String postalCode,
Nullable<System.Int32> salesTerritoryKey, System.String spanishCountryRegionName, System.String stateProvinceCode,
System.String stateProvinceName)
{
this.cityField = city;
this.countryRegionCodeField = countryRegionCode;
this.englishCountryRegionNameField = englishCountryRegionName;
this.frenchCountryRegionNameField = frenchCountryRegionName;
this.geographyKeyField = geographyKey;
this.postalCodeField = postalCode;
this.salesTerritoryKeyField = salesTerritoryKey;
this.spanishCountryRegionNameField = spanishCountryRegionName;
this.stateProvinceCodeField = stateProvinceCode;
this.stateProvinceNameField = stateProvinceName;
}
private System.String cityField;
public System.String City
{
get { return this.cityField; }
set { this.cityField = value; }
}
private System.String countryRegionCodeField;
public System.String CountryRegionCode
{
get { return this.countryRegionCodeField; }
set { this.countryRegionCodeField = value; }
}
private System.String englishCountryRegionNameField;
public System.String EnglishCountryRegionName
{
get { return this.englishCountryRegionNameField; }
set { this.englishCountryRegionNameField = value; }
}
private System.String frenchCountryRegionNameField;
public System.String FrenchCountryRegionName
{
get { return this.frenchCountryRegionNameField; }
set { this.frenchCountryRegionNameField = value; }
}
private System.Int32 geographyKeyField;
public System.Int32 GeographyKey
{
get { return this.geographyKeyField; }
set { this.geographyKeyField = value; }
}
private System.String postalCodeField;
public System.String PostalCode
{
get { return this.postalCodeField; }
set { this.postalCodeField = value; }
}
private Nullable<System.Int32> salesTerritoryKeyField;
public Nullable<System.Int32> SalesTerritoryKey
{
get { return this.salesTerritoryKeyField; }
set { this.salesTerritoryKeyField = value; }
}
private System.String spanishCountryRegionNameField;
public System.String SpanishCountryRegionName
{
get { return this.spanishCountryRegionNameField; }
set { this.spanishCountryRegionNameField = value; }
}
private System.String stateProvinceCodeField;
public System.String StateProvinceCode
{
get { return this.stateProvinceCodeField; }
set { this.stateProvinceCodeField = value; }
}
private System.String stateProvinceNameField;
public System.String StateProvinceName
{
get { return this.stateProvinceNameField; }
set { this.stateProvinceNameField = value; }
}
}
创建CRUD的存储过程
利用Repository Factory可以很方便的生成针对数据表的CRUD存储过程,可以生成Insert、Update、Delete、GetAll、GetOne、GetMany六种类型的存储过程。
Step1:选择Create CRUD Stored Procedures菜单
Step2:仍然是选择连接
Step3:选择要生成存储过程的数据表
Step4:设置是否生成上面所说的六种存储过程以及存储过程的名称
Step5:设置存储过程的输出文件
生成的存储过程部分代码:
----------------------------------------------------------------
-- [dbo].[DimGeography] Table
--
IF NOT EXISTS (SELECT NAME FROM sys.objects WHERE TYPE = 'P' AND NAME = 'InsertDimGeography')
BEGIN
EXEC('CREATE PROCEDURE [dbo].[InsertDimGeography] AS RETURN')
END
GO
ALTER PROCEDURE [dbo].[InsertDimGeography]
@city nvarchar(30) = NULL,
@countryRegionCode nvarchar(3) = NULL,
@englishCountryRegionName nvarchar(50) = NULL,
@frenchCountryRegionName nvarchar(50) = NULL,
@geographyKey int OUT,
@postalCode nvarchar(15) = NULL,
@salesTerritoryKey int,
@spanishCountryRegionName nvarchar(50) = NULL,
@stateProvinceCode nvarchar(3) = NULL,
@stateProvinceName nvarchar(50) = NULL
AS
BEGIN
SET NOCOUNT ON
BEGIN TRY
INSERT INTO [dbo].[DimGeography] ([City], [CountryRegionCode],
[EnglishCountryRegionName], [FrenchCountryRegionName], [PostalCode],
[SalesTerritoryKey], [SpanishCountryRegionName], [StateProvinceCode], [StateProvinceName])
VALUES (@city, @countryRegionCode, @englishCountryRegionName,
@frenchCountryRegionName, @postalCode, @salesTerritoryKey,
@spanishCountryRegionName, @stateProvinceCode, @stateProvinceName)
SET @geographyKey = SCOPE_IDENTITY()
END TRY
BEGIN CATCH
EXEC RethrowError;
END CATCH
SET NOCOUNT OFF
END
GO
结束语
使用Repository Factory生成业务实体和存储过程,就介绍到这里,限于篇幅,分成了上下两篇,其他内容放在微软轻量级“代码生成器”―Repository Factory使用(下)。