對於開發采用orm會帶來很大便利,orm即是數據表和實體對於關系框架。內部封裝增刪改查。它不僅適用於mvc,而且在winform和webform都適用。下面具體介紹orm是使用:
初步在winform為例使用,隨后在在mvc大量運用:
1,創建項目WindowsForm,並對其進行頁面布局
2,項目布局完成后,點擊項目右鍵,添加ADO.NET 實體生成模型
3,點擊空模型,創建實體.(從數據庫生成隨后會具體介紹)
4,生成edmx文件后,左擊文件添加實體類
5,對實體類進行構造:F4點開屬性進行設置ID(默認自增)
6,實現在數據庫中建立一個空數據庫,然后左擊新建實體選擇根據模型生成數據庫(當然還可以根據數據庫生成模型,接下章節進行講解)。這樣就可以自動生成數據庫以及對應的表(還可以建立數據表的關系,以及主外鍵約束,見下面章節)
7,此步操作后會生成一個sql文件,打開后並執行sql,即可完成數據表生成
以上構造成實體和數據庫,但是具體使用如下:
打開Model1.edmx的cs文件可以看到上下文和實體.在上下文中有一個命名Model1Container,負責實體和表結果聯系,類似網關功能.
實例化上下文;實現數據增刪改查
//展現用戶信息 public void Bind() { var user = from c in db.U_user select c; dataGridView1.DataSource = user; } //提交信息 private void button1_Click(object sender, EventArgs e) { U_user user=new U_user(); user.Name=txtname.Text; user.Pass=txtpass.Text; user.Type=txttype.Text; db.U_user.AddObject(user); db.SaveChanges(); this.Bind(); } //更新信息 private void button2_Click(object sender, EventArgs e) { int num = Convert.ToInt32(txtid.Text); var user =( from c in db.U_user where c.ID==num select c).FirstOrDefault<U_user>(); if (user != null) { user.Name = txtname.Text; user.Pass = txtpass.Text; user.Type = txttype.Text; db.SaveChanges(); } this.Bind(); } //刪除信息 private void button3_Click(object sender, EventArgs e) { int num = Convert.ToInt32(txtid.Text); var user = db.U_user.Where<U_user>(a => a.ID == num).FirstOrDefault<U_user>(); db.DeleteObject(user); db.SaveChanges(); } //獲取當前行 private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { txtid.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString(); txtname.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString(); txtpass.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString(); txttype.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString(); }
運行結果:
orm原理剖析:
選擇edmx文件,選擇打開方式可以查看具體xml文件:包含SSDL(表結構),CSDL(實體結構)和C-S mapping(映射關系)
SSDL(表結構)
<EntityType Name="U_user"> <Key> <PropertyRef Name="ID" /> </Key> <Property Name="ID" Type="int" StoreGeneratedPattern="Identity" Nullable="false" /> <Property Name="Name" Type="nvarchar" Nullable="false" MaxLength="12" /> <Property Name="Pass" Type="nvarchar" Nullable="false" MaxLength="12" /> <Property Name="Type" Type="nvarchar" Nullable="false" MaxLength="10" /> </EntityType>
CSDL(實體結構)
<EntityType Name="U_user"> <Key> <PropertyRef Name="ID" /> </Key> <Property Type="Int32" Name="ID" Nullable="false" annotation:StoreGeneratedPattern="Identity" /> <Property Type="String" Name="Name" Nullable="false" MaxLength="12" /> <Property Type="String" Name="Pass" Nullable="false" MaxLength="12" /> <Property Type="String" Name="Type" Nullable="false" MaxLength="10" /> </EntityType>
C-S mapping(映射關系)
<EntitySetMapping Name="U_user"> <EntityTypeMapping TypeName="IsTypeOf(Model1.U_user)"> <MappingFragment StoreEntitySet="U_user"> <ScalarProperty Name="ID" ColumnName="ID" /> <ScalarProperty Name="Name" ColumnName="Name" /> <ScalarProperty Name="Pass" ColumnName="Pass" /> <ScalarProperty Name="Type" ColumnName="Type" /> </MappingFragment> </EntityTypeMapping> </EntitySetMapping>