1.安装DbSharper
如果开发环境是VS2010需要修改注册表。
2.在VS中引用DbSharper.Library,并将复制本地设为True。
3.在web.config中加入
web.config
<configuration>
<configSections>
<sectionGroup name="dbSharper" type="DbSharper.Library.Configuration.ConfigurationSectionGroup, DbSharper.Library">
<sectionGroup name="cachingService" type="DbSharper.Library.Configuration.CachingServiceGroup, DbSharper.Library">
<section name="providers" type="DbSharper.Library.Configuration.CacheProvidersSection, DbSharper.Library" />
<section name="cacheSettings" type="DbSharper.Library.Configuration.CacheSettingsSection, DbSharper.Library" />
</sectionGroup>
</sectionGroup>
</configSections>
<dbSharper>
<cachingService>
<providers defaultProvider="InProcessCacheProvider">
<add name="InProcessCacheProvider" type="DbSharper.Library.Providers.InProcessCacheProvider, DbSharper.Library" />
</providers>
<!--设置DbSharper内置缓存-->
<cacheSettings enabled="false" defaultDuration="0"/>
</cachingService>
</dbSharper>
<!--Key(Core)值必须与VS中新建的DbSharper名称相同-->
<connectionStrings>
<add name="Core" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=UCar;Integrated Security=True;" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
<configSections>
<sectionGroup name="dbSharper" type="DbSharper.Library.Configuration.ConfigurationSectionGroup, DbSharper.Library">
<sectionGroup name="cachingService" type="DbSharper.Library.Configuration.CachingServiceGroup, DbSharper.Library">
<section name="providers" type="DbSharper.Library.Configuration.CacheProvidersSection, DbSharper.Library" />
<section name="cacheSettings" type="DbSharper.Library.Configuration.CacheSettingsSection, DbSharper.Library" />
</sectionGroup>
</sectionGroup>
</configSections>
<dbSharper>
<cachingService>
<providers defaultProvider="InProcessCacheProvider">
<add name="InProcessCacheProvider" type="DbSharper.Library.Providers.InProcessCacheProvider, DbSharper.Library" />
</providers>
<!--设置DbSharper内置缓存-->
<cacheSettings enabled="false" defaultDuration="0"/>
</cachingService>
</dbSharper>
<!--Key(Core)值必须与VS中新建的DbSharper名称相同-->
<connectionStrings>
<add name="Core" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=UCar;Integrated Security=True;" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
4.VS中新建项,选择DbSharper(保证)
web.config中数据库连接字符串的KEY要和此处新建的DbSharper文件同名,且指向的数据库需要安装过SP2补丁的。
在建立好的 DbSharper文件上点击右键选择“运行自定义工具”。
5.在数据库中建立数据库和表,可以在库的安全性中指定一个架构,此处我们新建一个Core的架构,方便日后维护。
6.建立存储过程,要按照DbSharper的规则命名,{表明}_{方法名},Produt_GetList (此存储过程建立在Core架构下),更新DbSharper后会自动建立{表明}的对象和{方法名}的方法,{方法名}返回{表明}对象。
public List<WebServiceTest.Core.Models.Core.ProdutItem> Proucts
{
get
{
return WebServiceTest.Core.DataAccess.Core.Produt.GetList().ToList();
}
}
{
get
{
return WebServiceTest.Core.DataAccess.Core.Produt.GetList().ToList();
}
}
ProdutItem为DbSharper生成的对象,GetList为生成的方法。