一、首先创建两张表,产品表T_Product和T_ProClass 在这里,我们把 ...

fishglugSoftware and s/w Development

Dec 13, 2013 (3 years and 10 months ago)

136 views

一、首先创建两张表,产品表
T_Product

T_ProClass


在这里,我们把两张表的关系,主外键设置好


二、现在我们通过
vs2012
添加
Entity Framework



选择
ADO.NET
实体数据模型
,添加


选择从数据库生成






连接成功



单击完成后,会生成如图所示的实体模型


我们可以看到在项目中生成了两个文件


Model.edmx

Model.Designer.cs
这两个文件分别用来描述实体以及他们之间关系,和自动
生成的
C#
代码,包括实体类的代码以及他们之
间的代码


三、框架已经搭建好,现在我们就可以通过
linq
语法实现对数据的查询,修改,删除,增
加等操作了

首先,我们来查询一下产品的列表

在页面中使用
Repeat
控件用来显示数据

<
form

id
="form1"

runat
="server">


<
div
>


<
table

border
="1">


<
tr
>


<
td
>

¨²

¡¤ID
</
td
>


<
td
>

¨²

¡¤

?

?
</
td
>


<
td
>

¨²

¡¤

?

?
</
td
>


<
td
>

¨²

¡¤

¤¨¤

Àe
</
td
>


</
tr
>


<
asp
:
Repeater

ID
="Repeater1"

runat
="server">


<
ItemTemplate
>


<
tr
>


<
td
>
<%
#
Eval(
"ID"
)
%>
</
td
>


<
td
>
<%
#
Eval(
"ProName"
)
%>
</
td
>


<
td
>
<%
#
Eval(
"ProPrice"
)
%>
</
td
>


<
td
>
<%
#
Eval(
"T_ProClass.ProC
lassName"
)
%>
</
td
>


</
tr
>


</
ItemTemplate
>


</
asp
:
Repeater
>


</
table
>


</
div
>

代码很简单,就是一个简单的查询

public

partial

class

Index

: System.Web.UI.
Page

{


EntityDataEntities

ef =
new

EntityDataEntities
();


protected

void

Page_Load(
objec
t

sender,
EventArgs

e)


{


if
(!IsPostBack)


{


ProBind();


}


}


public

void

ProBind()


{


var

proList =
from

s
in

ef.T_Product


select

s;



Repeater1.DataSource = proList;



Repeater1.DataBind();



}

}


在这里我们要重点说一下,因为数据库的主外键关系已经被映射到对象中,所以我们可以通
过对象直接调用和它关联的对象,在这里就是可以通过产品
T_Product
直接调用类别
T_ProClass

<%
#
Eval(
"T_ProClass.ProClassName"
)
%>


好,现在我们在来看看如何添加产品

在页面中添加文本框和按钮

<
form

id
="form1"

runat
="server">


<
div
>


<
table
>


<
tr
>


<
td
>

¨²

¡¤

?

?
</
td
>


<
td
>


<
asp
:
TextBox

ID
="txtProName"

runat
="server"></
asp
:
TextBox
></
td
>


</
tr
>


<
tr
>


<
td
>

¨²

¡¤

?

?
</
td
>


<
td
><
asp
:
TextBox

ID
="txtProPrice"

runat
="server"></
asp
:
TextBox
></
td
>


</
tr
>


<
tr
>


<
td
>

¨²

¡¤

¤¨¤

Àe
</
td
>


<
td
><
asp
:
Radi
oButtonList

ID
="RadioButtonList1"

runat
="server">


</
asp
:
RadioButtonList
>


</
td
>


</
tr
>


<
tr
>


<
td

colspan
="2">


<
asp
:
Button

ID
="Button1"

runat
="server"

Text
="

¬¨ª

¨®"

onclick
="Button1_Click"

/></
td
>


</
tr
>


</
table
>

</
div
>


后台代码

public

partial

class

Insert

: System.Web.UI.
Page

{


EntityDataEntities

ef =
new

EntityDataEntities
();


protected

void

Page_Load(
object

sender,
EventArgs

e)


{


if
(!IsPostBack)


{


bindProClass();


}




}


protected

void

bindProClass()


{


var

query =
from

ps
in

ef.T_ProClass


select

ps;


RadioButtonList1.DataSource = query;


RadioButtonList1.DataTextField =
"proClassName"
;


RadioButtonList1.DataValueFiel
d =
"ID"
;


RadioButtonList1.DataBind();


}


protected

void

Button1_Click(
object

sender,
EventArgs

e)


{


T_Product

product =
new

T_Product
();


product.ProClass =
Convert
.ToInt32(RadioButtonList1.SelectedValue);


product.
ProName = txtProName.Text;


product.ProPrice =
Convert
.ToDecimal(txtProPrice.Text);


ef.T_Product.AddObject(product);


ef.SaveChanges();


ef.Dispose();


Response.Write(
"<script>alert('

¬¨ª

¨®

¨¦

|');location.href='Index.a
spx'</script>"
);


}


修改和添加很类似

页面代码:

<
form

id
="form1"

runat
="server">


<
div
>


<
table
>


<
tr
>


<
td
>

¨²

¡¤

?

?
</
td
>


<
td
>


<
asp
:
TextBox

ID
="txtProName"

runat
="server"></
asp
:
TextBox
></
td
>


</
tr
>


<
tr
>


<
td
>

¨²

¡¤

?

?
</
td
>



<
td
><
asp
:
TextBox

ID
="txtProPrice"

runat
="server"></
asp
:
TextBox
></
td
>


</
tr
>


<
tr
>


<
td
>

¨²

¡¤

¤¨¤

Àe
</
td
>


<
td
><
asp
:
RadioButtonList

ID
="RadioButtonList1"

runat
="server">


</
asp
:
RadioButtonList
>


</
td
>


</
tr
>


<
tr
>


<
td

c
olspan
="2">


<
asp
:
Button

ID
="Button1"

runat
="server"

Text
="

T

?"

onclick
="Button1_Click"

/></
td
>


</
tr
>


</
table
>


</
div
>


</
form
>

后台的代码


EntityDataEntities

ef =
new

EntityDataEntities
();


protected

void

Page_Load(
object

sender,
Even
tArgs

e)


{


if

(!IsPostBack)


{


int

id =
Convert
.ToInt32(Request.QueryString[
"proId"
].ToString());


bindProClass();


ProdectById(id);


}




}


protected

void

bindProClass()


{


var

query =
from

ps
in

ef.T_ProClass


select

ps;


RadioButtonList1.DataSource = query;


RadioButtonList1.DataTextField =
"proClassName"
;


RadioButtonList1.DataValueField =
"ID"
;


RadioButtonList1.DataBind();


}



protected

void

ProdectById(
int

id)


{




var

product = (
from

p
in

ef.T_Product


where

p.ID == id


select

p).Single();


RadioButtonList1.SelectedValue = product.ProClass.ToString();


txtProName.Text = product.ProName;


txtProPrice.Text = product.ProPrice.ToString();


}


protected

void

Button1_Click(
object

sender,
EventArgs

e)


{


int

id =
Convert
.ToInt32(Request.QueryString[
"proId"
].ToString());


var

produc
t = (
from

p
in

ef.T_Product


where

p.ID == id


select

p).Single();


product.ProClass =
Convert
.ToInt32(RadioButtonList1.SelectedValue);


product.ProName = txtProName.Text;


product.ProPrice =

Convert
.ToDecimal(txtProPrice.Text);


ef.SaveChanges();


ef.Dispose();


Response.Write(
"<script>alert('

T

?

¨¦

|');location.href='Index.aspx'</script>"
);


}

删除的代码


EntityDataEntities

ef =
new

EntityDataEntities
();


protected

void

Page_Load(
object

sender,
EventArgs

e)


{


if

(!IsPostBack)


{


int

id =
Convert
.ToInt32(Request.QueryString[
"proId"
].ToString());


var

product = (
from

p
in

ef.T_Product


where

p.ID == id


select

p).Single();


ef.DeleteObject(product);


ef.SaveChanges();


ef.Dispose();


Response.Write(
"<script>alert('

¦?

y

¨¦

|');location.href='Index.aspx'</script>"
);


}




}


之记住每次
ef.SaveChanges();