本篇演示一对多关系,和两个表联合查询。
使用已有数据库,我们演示一个家庭理财的小程序
本文参考Teddy's Knowledge Base的相关文章,这篇文章只演示单表操作.
Step1: 创建数据库Family
Account表:记录每一笔消费,列ConsumeCategory是消费的类别。
ConsumeCatagory表:消费类别。
ConsumeCatagory----Account(1:n)
if exists ( select * from dbo.sysobjects where id = object_id (N ' [dbo].[FK_Account_AccountType] ' )
and OBJECTPROPERTY (id, N ' IsForeignKey ' ) = 1 )
ALTER TABLE [ dbo ] . [ Consume ] DROP CONSTRAINT FK_Account_AccountType
GO
if exists ( select * from dbo.sysobjects where id = object_id (N ' [dbo].[Consume] ' )
and OBJECTPROPERTY (id, N ' IsUserTable ' ) = 1 )
drop table [ dbo ] . [ Consume ]
GO
if exists ( select * from dbo.sysobjects where id = object_id (N ' [dbo].[ConsumeCatagory] ' )
and OBJECTPROPERTY (id, N ' IsUserTable ' ) = 1 )
drop table [ dbo ] . [ ConsumeCatagory ]
GO
CREATE TABLE [ dbo ] . [ Consume ] (
[ ID ] [ int ] IDENTITY ( 1 , 1 ) NOT NULL ,
[ ConsumeCatagory ] [ int ] NULL ,
[ ConsumeDateTime ] [ datetime ] NULL ,
[ ConsumeMoney ] [ money ] NULL ,
[ ConsumeDescription ] [ ntext ] COLLATE Chinese_PRC_CI_AS NULL ,
[ ConsumeUser ] [ nchar ] ( 10 ) COLLATE Chinese_PRC_CI_AS NULL ,
[ ConsumeType ] [ nchar ] ( 10 ) COLLATE Chinese_PRC_CI_AS NULL
) ON [ PRIMARY ] TEXTIMAGE_ON [ PRIMARY ]
GO
CREATE TABLE [ dbo ] . [ ConsumeCatagory ] (
[ ID ] [ int ] IDENTITY ( 1 , 1 ) NOT NULL ,
[ Type ] [ nvarchar ] ( 50 ) COLLATE Chinese_PRC_CI_AS NULL ,
[ Description ] [ nvarchar ] ( 50 ) COLLATE Chinese_PRC_CI_AS NULL
) ON [ PRIMARY ]
GO
ALTER TABLE [ dbo ] . [ Consume ] ADD
CONSTRAINT [ PK_Account ] PRIMARY KEY CLUSTERED
(
[ ID ]
) ON [ PRIMARY ]
GO
ALTER TABLE [ dbo ] . [ ConsumeCatagory ] ADD
CONSTRAINT [ PK_AccountType ] PRIMARY KEY CLUSTERED
(
[ ID ]
) ON [ PRIMARY ]
GO
ALTER TABLE [ dbo ] . [ Consume ] ADD
CONSTRAINT [ FK_Account_AccountType ] FOREIGN KEY
(
[ ConsumeCatagory ]
) REFERENCES [ dbo ] . [ ConsumeCatagory ] (
[ ID ]
)
GO
Step2:
(1)安装下载的Nbear组件下dist\SetupNBearVsPlugin.exe插件(可自动生成实体类)
(2)打开VS2005,新建一个空的解决方案,添加两个C#类库工程EntityDesigns和Entities,删除自动生成的class1.cs.
(3)添加一个website的网站项目。
(4)在Entities下添加Entities.cs, 在网站根目录下添加web.config文件和EntityConfig.xml文件.
(5)修改web.config文件
< configSections >
< section name ="entityConfig" type ="NBear.Common.EntityConfigurationSection, NBear.Common" />
</ configSections >
< entityConfig >
< includes >
< add key ="Sample Entity Config" value ="~/EntityConfig.xml" />
</ includes >
</ entityConfig >
< appSettings />
< connectionStrings >
< add name ="DbName" connectionString ="Server=WDS;Database=NBearDB;Uid=sa;Pwd=sa"
providerName ="NBear.Data.SqlServer.SqlDbProvider" />
</ connectionStrings >(6)在EntityDesigns目录下添加EntityDesignToEntityConfig.xml文件,内容如下:
<? xml version="1.0" encoding="utf-8" ?>
< EntityDesignToEntityConfiguration xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd ="http://www.w3.org/2001/XMLSchema" >
< CompileMode > Debug </ CompileMode >
< InputDllName > EntityDesigns.dll </ InputDllName > //实体设计类
< OutputNamespace > Entities </ OutputNamespace > //实体类
< OutputLanguage > C# </ OutputLanguage > //输出语言
<!-- <OutputCodeFileEncoding>utf-8</OutputCodeFileEncoding> -->
< EntityCodePath > ..\Entities\Entities.cs </ EntityCodePath > //实体类文件
< EntityConfigPath > ..\website\EntityConfig.xml </ EntityConfigPath >
< SqlSync enable ="false" > //更改实体时,是否同步更新数据库,建议设为false,否则将清空现有数据库的数据
< SqlServerFolder > C:\Program Files\Microsoft SQL Server\80\Tools\Binn </ SqlServerFolder >
< ServerName > WDS </ ServerName >
< UserID > sa </ UserID >
< Password > sa </ Password >
< DatabaseName > NBearDB </ DatabaseName >
</ SqlSync >
</ EntityDesignToEntityConfiguration >(7)在EntityDesigns目录下添加EntityDesigns.cs
(8)打开下载的Nbear组件下dist\NBear.Tools.DbToEntityDesign.exe程序 ,设置连接字符串,我的是这样:
Server=WDS;Database=NBearDB;Uid=sa;Pwd=sa 点击connect, 选择要生成的表,点击Generate Entities Design.将会得到下面的代码:
生成实体如下
{
[PrimaryKey]
int ID { get ; }
int ? ConsumeCatagory { get ; set ; }
DateTime ? ConsumeDateTime { get ; set ; }
decimal ? ConsumeMoney { get ; set ; }
[SqlType( " ntext " )]
string ConsumeDescription { get ; set ; }
[SqlType( " nchar(10) " )]
string ConsumeUser { get ; set ; }
[SqlType( " nchar(10) " )]
string ConsumeType { get ; set ; }
}
public interface ConsumeCatagory : Entity
{
[PrimaryKey]
int ID { get ; }
[SqlType( " nvarchar(50) " )]
string Type { get ; set ; }
[SqlType( " nvarchar(50) " )]
string Description { get ; set ; }
}
拷贝上面的代码到EntityDesigns.cs,为EntityDesigns添加NBear.Common.Design引用.
修改EntityDesigns.cs实体为
{
[PrimaryKey]
int ID { get ; }
// int? ConsumeCatagory { get; set; }
DateTime ? ConsumeDateTime { get ; set ; }
decimal ? ConsumeMoney { get ; set ; }
[SqlType( " ntext " )]
string ConsumeDescription { get ; set ; }
[SqlType( " nchar(10) " )]
string ConsumeUser { get ; set ; }
[SqlType( " nchar(10) " )]
string ConsumeType { get ; set ; }
[FkReverseQuery(LazyLoad = true )] // 这句指明可反向查询,如可直接得到consume对象catagory.Type
[MappingName( " ConsumeCatagory " )] // 只根据consume实体的ConsumeCatagory来关联,既ConsumeCatagory是外键。
ConsumeCatagory catagory
{
get ;
set ;
}
}
public interface ConsumeCatagory : Entity
{
[PrimaryKey]
int ID { get ; }
[SqlType( " nvarchar(50) " )]
string Type { get ; set ; }
[SqlType( " nvarchar(50) " )]
string Description { get ; set ; }
[FkQuery( " ConsumeCatagory " )] // 根据Consume实体的ConsumeCatagory属性来关联
Consume[] consume
{
get ;
set ;
}
}
(9)编译EntityDesigns工程,编译成功,刚才添加Entities.cs,EntityConfig.xml文件将被自动填入正确的内容。
(10)为Entities工程引用NBear.Common,然后编译Entities工程。
(11)为WebSite添加Entities项目编译后的Entities.dll.再引用NBear.Data
(12)开始使用设计好的实体。
在default页添加两个gridview, gvdAccountType显示消费类别, gvdAccount显示消费明细。代码如下
aspx页面的设置
运行得到如下界面
我们为什看到上面消费明细里的类别显示的是类别名,而不是1,2,3,4呢?是因为我们利用实体关系进行了自动的联合查询,
绑定时我们绑定到Consume实体的catagory属性的Type属性就可以了(上面代码突出显示红色的地方)。
注:当我为Consume实体加上
[FkReverseQuery(LazyLoad = true)]
[MappingName("ConsumeCatagory")]
ConsumeCatagory catagory
{
get;
set;
}
以上代码时,我必须注释掉 int? ConsumeCatagory { get; set; }这句,不然添加数据会出错。
现在我们添加一条类别为买菜的数据时只能这样(不能直接这样用today.ConsumeCatagory=1, 因为已经没有这个属性了),我们得先找到类别为买菜的对象,然后付给consume对象的catagory属性就可以了。(奇怪为什么需要注释掉,或者还有别的办法?)
现在我们假如想得到买菜类别的所有消费,我们只需这样
本文转自敏捷的水博客园博客,原文链接http://www.cnblogs.com/cnblogsfans/archive/2007/12/02/980076.html如需转载请自行联系原作者
王德水