[C#][EF]利用WCF存取EDM
這篇練習利用WCF進行EDM存取相關操作,自己記錄一下。
假設你的WCF應用程式專案已完成並成功加入Web專案中(這裡就不討論WCF相關細節了)。
IService1.cs
服務合約(ServiceContract):IService1定義了相關的作業合約,服務合約由多個作業合約所組成。
[ServiceContract]
public interface IService1
{
// TODO: 在此新增您的服務作業
[OperationContract]
string InsertUser(USER_M name);
[OperationContract]
string UpdateUser(USER_M name);
[OperationContract]
string DeleteUser( string name );
[OperationContract]
USER_M GetPhone( string name );
[OperationContract]
List<USER_M> GetAllData();
}
資料合約(DataContract):定義資料格式,確保資料能夠被序列化分解,
然後在服務和客戶端應用程式之間傳輸,經過反序列化還原。
//使用下列範例中所示的資料合約,新增複合型別至服務作業。
[DataContract]
public class UserPhone
{
private USER_M _user;
[DataMember]
public USER_M Phone
{
get
{
return _user;
}
set
{
_user = value;
}
}
}
[DataContract]
public class ListUser
{
private string _name;
private string _zipcode;
private string _phone;
[DataMember]
public string Nmae
{
get
{
return _name;
}
set
{
_name = value;
}
}
[DataMember]
public string ZipCode
{
get
{
return _zipcode;
}
set
{
_zipcode = value;
}
}
[DataMember]
public string Phone
{
get
{
return _phone;
}
set
{
_phone = value;
}
}
}
Service1.svc.cs:實作該IService1作業合約內容
// 注意: 您可以使用 [重構] 功能表上的 [重新命名] 命令同時變更程式碼、svc 和組態檔中的類別名稱 "Service1"。
public class Service1 : IService1
{
#region IService1 成員
public string InsertUser( USER_M user )
{
using( demoEntities db = new demoEntities() )
{
try
{
db.AddToUSER_M( user );
db.SaveChanges();
return "新增成功";
}
catch( Exception ex )
{
return "新增失敗:" +ex.Message;
}
}
}
public string UpdateUser( USER_M user )
{
using( demoEntities db = new demoEntities() )
{
try
{
USER_M _user = db.USER_M.Single( o => o.NAME == user.NAME );
_user.PHONE = user.PHONE;
_user.ZIPCODE =user.ZIPCODE;
db.SaveChanges();
return "更新成功";
}
catch( Exception ex )
{
return "更新失敗:" + ex.Message;
}
}
}
public string DeleteUser( string name )
{
using( demoEntities db = new demoEntities() )
{
try
{
var q = db.USER_M.Where( o => o.NAME == name ).FirstOrDefault();
USER_M user = q;
db.DeleteObject( user );
db.SaveChanges();
return "刪除成功";
}
catch( Exception ex )
{
return "刪除失敗:" + ex.Message;
}
}
}
public USER_M GetPhone( string name )
{
demoEntities db = new demoEntities();
var q = db.USER_M.Where( o => o.NAME == name ).FirstOrDefault();
USER_M _user = q;
return _user;
}
public List<USER_M> GetAllData()
{
demoEntities db = new demoEntities();
var q = from t in db.USER_M
select t;
List<USER_M> ListUser = q.ToList();
return ListUser;
}
#endregion
}
TestControl.cs
#region 利用WCF存取EDM
public USER_M GetPhone( string name )
{
Service1 sv = new Service1();
return sv.GetPhone(name);
}
public List<USER_M> GetAllData()
{
Service1 sv = new Service1();
return sv.GetAllData();
}
public string Update( USER_M user )
{
Service1 sv = new Service1();
return sv.UpdateUser( user );
}
public string Insert( USER_M user )
{
Service1 sv = new Service1();
return sv.InsertUser( user );
}
public string Delete( string name )
{
Service1 sv = new Service1();
return sv.DeleteUser(name);
}
#endregion
WCFandEDM.aspx.cs
protected void Button1_Click( object sender, EventArgs e )
{
if( TextBox1.Text.Length > 0 )
{
//取得電話號碼
TestControl testcontrol = new TestControl();
Label1.Text = testcontrol.GetPhone(TextBox1.Text.Trim()).PHONE;
}
}
protected void Button2_Click( object sender, EventArgs e )
{
//取得全部資料
TestControl testcontrol = new TestControl();
GridView1.DataSource = testcontrol.GetAllData();
GridView1.DataBind();
}
protected void GridView1_RowEditing( object sender, GridViewEditEventArgs e )
{
GridView1.EditIndex = e.NewEditIndex;
TestControl testcontrol = new TestControl();
GridView1.DataSource = testcontrol.GetAllData();
GridView1.DataBind();
}
protected void GridView1_RowUpdating( object sender, GridViewUpdateEventArgs e )
{
//更新資料
TestControl testcontrol = new TestControl();
string Name= ((Label)GridView1.Rows[e.RowIndex].FindControl("Label1")).Text;
string ZipCode = ( ( TextBox ) GridView1.Rows[ e.RowIndex ].FindControl( "TextBox2" ) ).Text;
string Phone = ( ( TextBox ) GridView1.Rows[ e.RowIndex ].FindControl( "TextBox3" ) ).Text;
USER_M _user = testcontrol.GetPhone( Name );
_user.PHONE = Phone;
_user.ZIPCODE = ZipCode;
Label1.Text = testcontrol.Update( _user );
GridView1.EditIndex = -1;
GridView1.DataSource = testcontrol.GetAllData();
GridView1.DataBind();
}
protected void GridView1_RowCancelingEdit( object sender, GridViewCancelEditEventArgs e )
{
GridView1.EditIndex = -1;
TestControl testcontrol = new TestControl();
GridView1.DataSource = testcontrol.GetAllData();
GridView1.DataBind();
}
protected void GridView1_RowDeleting( object sender, GridViewDeleteEventArgs e )
{
//刪除資料
TestControl testcontrol = new TestControl();
string Name = ( ( Label ) GridView1.Rows[ e.RowIndex ].FindControl( "Label1" ) ).Text;
Label1.Text = testcontrol.Delete( Name );
GridView1.DataSource = testcontrol.GetAllData();
GridView1.DataBind();
}
protected void Button3_Click( object sender, EventArgs e )
{
//新增資料
TestControl testcontrol = new TestControl();
string Name = TextBox2.Text.Trim();
string ZipCode = TextBox3.Text.Trim();
string Phone = TextBox4.Text.Trim();
USER_M _user = new USER_M();
_user.NAME = Name;
_user.ZIPCODE = ZipCode;
_user.PHONE = Phone;
Label1.Text = testcontrol.Insert( _user );
GridView1.DataSource = testcontrol.GetAllData();
GridView1.DataBind();
}
結果:
Select
取得 rico 電話號碼。
取得全部資料。
Update
更新 dou 郵遞區號。
Insert
新增 test 。
Delete
刪除 test 。
參考