<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      [EF]讓Entity framework支持多數據庫

      讓Entity framework支持多數據庫

      羅朝輝 (http://kesalin.cnblogs.com/)

      本文遵循“署名-非商業用途-保持一致”創作公用協議
       

      EF對Sql Server的支持非常好,無論是Code First,還是 Model First 還是 Database First 都支持的很好,但是對非微軟系數據庫的支持就不那么友好了,現在唯一能保證的是對大部分數據庫的 Database First 支持的很好。所以在這里,我們讓 Entity framework 支持多數據庫實現的思路就是基于 Database First 的。首先在各數據庫中創建好數據庫表(這里有很多講究的地府,字段類型必須一致,可以使用Power Designer工具來簡化手工勞動),再基于某一數據庫生成概念模型,存儲模型以及映射關系,然后拷貝生成的存儲模型文件并修改,使之能與其他數據庫匹配起來,從而獲得對多數據庫的支持。

      本示例演示了對Sql Server 2008和MySQL 5.5兩種書庫的支持,使用的 MySQL Connector Net 6.3.5。請參考前文安裝相關的軟件。下面講述具體步驟:

      1,分別在 Sql Server 2008 和 MySQL 5.5 建立數據庫 school及表 student(推薦使用小寫,MySQL默認使用小寫),student表只包含三個字段:Id(主鍵),Name 和 Age。請注意兩個表的數據類型必須完全一致!

      Sql Server 2008表:

      MySql 表:

       

      2,然后分別在兩個表中插入不同的測試數據,也可以在代碼中寫插入數據,這里為了簡化,直接使用數據庫管理工具插入測試數據。

      3,新建C#控制臺程序 EFMutilpleDatabase:

       

      4,右擊項目名,向其中添加類型為ADO.NET Entity Data Model的新Item:StudentModel.edmx:

      選擇從數據庫產生:

      然后新建 Connection,首先我們使用 MySQL數據庫,設置連接到MySQL的Connection,數據庫表選擇 school,這樣向導就會自動為我們生成概念模型,存儲模型以及映射關系,connection string等:

      選擇需要用到的數據庫表:

       

      5,至此向導工作完成,我們先來看看向導為我們生成的文件:

      App.Config:數據庫連接相關的配置;

      StudentModel.edmx:概念模型,存儲模型,映射關系等都自動生成在該文件中;

      StudentModel.Designer.cs:自動生成的代碼,通過這些自動生成的數據對象類,我們就可以直接操作數據庫。

       

      6,為了方便后面支持多數據庫,我們現在在App.Config文件中修改默認 connection string的名字為:schoolEntitiesMySQL。

      <?xml version="1.0" encoding="utf-8"?>
      <configuration>
      <connectionStrings>
      <add name="schoolEntitiesMySQL" connectionString="metadata=res://*/StudentModel.csdl|res://*/StudentModel.ssdl|res://*/StudentModel.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot;server=localhost;User Id=root;password=yourpwd;Persist Security Info=True;database=school&quot;" providerName="System.Data.EntityClient" />
      </connectionStrings>
      </configuration>


      7,下面我們來編寫測試與MySQL的連接的代碼。相工程中添加 reference:System.Configuration,然后修改 Program.cs為:

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Data;
      using System.Data.Objects;
      using System.Configuration;

      namespace EFMultipleDatabase
      {
      class Program
      {
      private static void ProcessDatabase(string connectionStringName)
      {
      string connectionString = ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
      using (var queryContext = new schoolEntities(connectionString))
      {
      Console.WriteLine(">> All Student:");

      queryContext.Connection.Open();

      // Query
      //
      using (var transaction = queryContext.Connection.BeginTransaction())
      {
      var people = from p in queryContext.student orderby p.Age select p;
      foreach (var p in people)
      {
      Console.WriteLine(" {0}, Age: {1}", p.Name, p.Age);
      }

      transaction.Commit();
      }

      queryContext.Dispose();
      }
      }

      static void Main(string[] args)
      {
      Console.WriteLine(" ========MySQL=====");
      ProcessDatabase("schoolEntitiesMySQL");

      Console.ReadLine();
      }
      }
      }

      在上面的代碼中,我們是根據讀取 App.Config 中的 connection string 配置來決定連接到哪一個數據庫的,這樣的設計方便我們添加與其他數據庫連接配置,以支持多種數據庫。

      8,下面我們來讓程序支持 Sql Server 2008。首先,需要手動創建存儲模型文件,在項目中新建名為StudentModel.SqlServer.ssdl 的xml文件。然后右擊StudentModel.edmx選擇 Open With XML(Text) Editor,將<!-- SSDL content -->之后介于 <edmx:StorageModels> … </edmx:StorageModels>之間的內容拷貝至新建的StudentModel.SqlServer.ssdl文件中,這樣StudentModel.SqlServer.ssdl的內容應該如下:

      <?xml version="1.0" encoding="utf-8" ?>
      <Schema Namespace="schoolModel.Store" Alias="Self" Provider="MySql.Data.MySqlClient" ProviderManifestToken="5.1" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2009/02/edm/ssdl">
      <EntityContainer Name="schoolModelStoreContainer">
      <EntitySet Name="student" EntityType="schoolModel.Store.student" store:Type="Tables" Schema="school" />
      </EntityContainer>
      <EntityType Name="student">
      <Key>
      <PropertyRef Name="Id" />
      </Key>
      <Property Name="Id" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
      <Property Name="Name" Type="varchar" MaxLength="45" />
      <Property Name="Age" Type="umediumint" />
      </EntityType>
      </Schema>


      9,緊接著我們來修改 StudentModel.SqlServer.ssdl 存儲模型文件,使之能與匹配Sql Server 2008數據庫表。首先我們需要將 Provider 和 ProviderManifestToken修改為:

      Provider="System.Data.SqlClient" ProviderManifestToken="2008"
      

      這表示該存儲模型是基于 Sql Server 2008 數據庫的。然后將各個字段的 Type 修改為Sql Server 2008支持的數據類型(MySQL使用的數據類型肯定不會與Sql Server 2008完全相同),并將該存儲模型放到輸出目錄下,修改該文件的屬性 Copy to Output Directory 為 Copy Always。。修改之后的內容應如下:

      <?xml version="1.0" encoding="utf-8" ?>
      <Schema Namespace="schoolModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2008" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2009/02/edm/ssdl">
      <EntityContainer Name="schoolModelStoreContainer">
      <EntitySet Name="student" EntityType="schoolModel.Store.student" store:Type="Tables" Schema="dbo" />
      </EntityContainer>
      <EntityType Name="student">
      <Key>
      <PropertyRef Name="Id" />
      </Key>
      <Property Name="Id" Type="int" Nullable="false"/>
      <Property Name="Name" Type="nchar" MaxLength="45" />
      <Property Name="Age" Type="int" />
      </EntityType>
      </Schema>


      10,這樣我們就完成了對存儲模型的修改,下面我們來在App.config中增加對Sql Server 2008數據庫的連接 string。以下是修改之后的App.config:

      <?xml version="1.0" encoding="utf-8"?>
      <configuration>
      <connectionStrings>
      <add name="schoolEntitiesMySQL" connectionString="metadata=res://*/StudentModel.csdl|res://*/StudentModel.ssdl|res://*/StudentModel.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot;server=localhost;User Id=root;password=yourpwd;Persist Security Info=True;database=school&quot;" providerName="System.Data.EntityClient" />
      <add name="schoolEntitiesSqlServer" connectionString="metadata=res://*/StudentModel.csdl|StudentModel.SqlServer.ssdl|res://*/StudentModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.;initial catalog=school;persist security info=True;user id=sa;password=
      yourpwd
      ;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
      </connectionStrings>
      </configuration>


      11,至此配置工作完成,我們只需在 main()中調用如下語句即可測試與Sql Server 2008數據庫的連接:

              static void Main(string[] args)
      {
      Console.WriteLine(" ========MySQL=====");
      ProcessDatabase("schoolEntitiesMySQL");

      Console.WriteLine(" ========SQL Server 2008=====");
      ProcessDatabase("schoolEntitiesSqlServer");

      Console.ReadLine();
      }

      編譯運行,結果如下:

       

      總結:

      上面的過程是先生成一致的數據庫表,然后通過某種數據庫生成概念模型,存儲模型以及映射關系,然后拷貝并修改存儲模型,使之與其他數據庫匹配,從而完成對多數據庫的支持。

      posted @ 2012-03-20 19:52  飄飄白云  閱讀(10780)  評論(6)    收藏  舉報
      本博客遵循 Creative Commons License “署名-非商業用途-保持一致”創作共用協議。 與我聯系
      主站蜘蛛池模板: 自拍偷自拍亚洲精品播放| 国产欧美日韩另类精彩视频| 激情久久av一区av二区av三区| 中文字幕日韩区二区三区| 人妻日韩人妻中文字幕| 国产亚洲精品VA片在线播放| 有码中文字幕一区三区| 日韩中文字幕av有码| 国产美女久久久亚洲综合| 精品国际久久久久999波多野| 亚洲成人av综合一区| 欧美激情内射喷水高潮| 本道久久综合无码中文字幕| 国产成人精品一区二三区| 丰满少妇内射一区| 日韩有码中文字幕av| 少妇伦子伦情品无吗| 秋霞人妻无码中文字幕| 狠狠色噜噜狠狠狠888米奇视频| 亚洲中文无码av永久不收费| 伊人成色综合人夜夜久久| 久热伊人精品国产中文| 欧美日韩精品一区二区视频| 麻豆精品一区二正一三区| 99RE8这里有精品热视频| 男女动态无遮挡动态图| 亚洲开心婷婷中文字幕| 尹人香蕉久久99天天拍欧美p7 | 久久精品国产精品第一区| 国产中文字幕精品视频| 亚洲成AV人片在线观高清| 99久久精品国产熟女拳交| 国产欧美精品一区aⅴ影院| 波多野结衣av无码| 蜜桃无码一区二区三区| 无码人妻aⅴ一区二区三区蜜桃| 中文字幕无码视频手机免费看| L日韩欧美看国产日韩欧美| 麻豆精品在线| 久久精品亚洲中文字幕无码网站| 国产中文字幕精品视频|