程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> Entity Framework With Oracle,entityframework

Entity Framework With Oracle,entityframework

編輯:關於.NET

Entity Framework With Oracle,entityframework


參考頁面:

http://www.yuanjiaocheng.net/Entity/first.html

http://www.yuanjiaocheng.net/Entity/jieshao.html

http://www.yuanjiaocheng.net/entity/tixijiegou.html

http://www.yuanjiaocheng.net/entity/setenvrionment.html

http://www.yuanjiaocheng.net/entity/createdatamodel.html

雖然EF6都快要出來了,但是對於Oracle數據庫,仍然只能用DB first和Model First來編程,不能用Code First真是一個很大的遺憾啊。

好了,廢話少說,我們來看看EF中是如何用DB first和Model First來對Oracle編程的。

首先我們要下載ODP.NET這個數據驅動程序,下載鏈接:http://www.oracle.com/technetwork/topics/dotnet/index-085163.html

安裝成功後,我們在VS連接Oracle數據庫時就可以選擇ODP.NET了,如圖:

Model First

模型優先是先建立數據模型,然後再根據模型生成相應的數據庫腳本,然後再根據腳本生成數據庫。

在項目中新增一個ADO.NET實體模型:OracleModel.edmx,選擇“空模型”,再新新建兩個實體:Destination與Lodging,如圖:

為了看清這兩個模型中屬性的數據類型,我把他們生成的類也貼出來一下:

View Code
 public partial class Destination
    {
        public Destination()
        {
            this.Lodging = new HashSet<Lodging>();
        }
    
        public int DestinationId { get; set; }
        public string Name { get; set; }
        public string Country { get; set; }
        public byte Photo { get; set; }
        public string Description { get; set; }
    
        public virtual ICollection<Lodging> Lodging { get; set; }
    }

 public partial class Lodging
    {
        public int LodgingId { get; set; }
        public string Name { get; set; }
        public string Owner { get; set; }
        public bool IsResort { get; set; }
        public decimal MilesFromNearestAirport { get; set; }
        public int DestinationDestinationId { get; set; }
    
        public virtual Destination Destination { get; set; }
    }

實體模型的空白處,右鍵-屬性,在打開的OracleModel模型屬性窗口,設置一些屬性,將DDL生成模板改成:SSDLToOracle.tt (VS),數據庫架構名稱改成:GYOUNG(這是我自己測試的Oracle數據庫的用戶名,大家可根據自己的更改),數據庫生成工作流改成:Generate Oracle Via T4 (TPT).xaml (VS)

為了讓EF更好的明白.NET中的數據類型與Oracle中數據類型間的對應關系。我們可以將下面的配置文件加到app.config中。

  <oracle.dataaccess.client>
    <settings>
      <add name="bool" value="edmmapping number(1,0)" />
      <add name="byte" value="edmmapping number(3,0)" />
      <add name="int16" value="edmmapping number(4,0)" />
      <add name="int32" value="edmmapping number(9,0)" />
      <add name="int64" value="edmmapping number(18,0)" />
    </settings>
  </oracle.dataaccess.client>

現在我們就可以生成數據庫的相應腳本了。

在空白處右鍵,選擇“根據模型生成數據庫”

然後建立好數據連接,如圖:

點擊下一步,然後就會生成相應的數據腳本。

View Code
-- Creating table 'Destinations'
CREATE TABLE "GYOUNG"."Destinations" (
   "DestinationId" NUMBER(9,0) NOT NULL,
   "Name" NCLOB NOT NULL,
   "Country" NCLOB NOT NULL,
   "Photo" NUMBER(3,0) NOT NULL,
   "Description" NCLOB NOT NULL
);

-- Creating table 'Lodgings'
CREATE TABLE "GYOUNG"."Lodgings" (
   "LodgingId" NUMBER(9,0) NOT NULL,
   "Name" NCLOB NOT NULL,
   "Owner" NCLOB NOT NULL,
   "IsResort" NUMBER(1,0) NOT NULL,
   "MilesFromNearestAirport" NUMBER(38,0) NOT NULL,
   "DestinationDestinationId" NUMBER(9,0) NOT NULL
);


-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on "DestinationId"in table 'Destinations'
ALTER TABLE "GYOUNG"."Destinations"
ADD CONSTRAINT "PK_Destinations"
   PRIMARY KEY ("DestinationId" )
   ENABLE
   VALIDATE;


-- Creating primary key on "LodgingId"in table 'Lodgings'
ALTER TABLE "GYOUNG"."Lodgings"
ADD CONSTRAINT "PK_Lodgings"
   PRIMARY KEY ("LodgingId" )
   ENABLE
   VALIDATE;


-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on "DestinationDestinationId" in table 'Lodgings'
ALTER TABLE "GYOUNG"."Lodgings"
ADD CONSTRAINT "FK_DestinationLodging"
   FOREIGN KEY ("DestinationDestinationId")
   REFERENCES "GYOUNG"."Destinations"
       ("DestinationId")
   ENABLE
   VALIDATE;

-- Creating index for FOREIGN KEY 'FK_DestinationLodging'
CREATE INDEX "IX_FK_DestinationLodging"
ON "GYOUNG"."Lodgings"
   ("DestinationDestinationId");

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------

我們只要將腳本到數據庫中執行一下就可以生成相應的表了。分析一下生成的SQL語句,有主鍵,外鍵,但並沒有為主鍵設置自增長。Oracle設置自增長也是一個很蛋疼的問題,要通過設置相應的Sequences和Triggers來實現,習慣了SQL SERVER的IDENTITY,對於這個還真不爽。這裡我們不管它,就自己插入主鍵好了。下面是測試代碼:

View Code
 using (OracleModelContainer context = new OracleModelContainer())
            {
                var destination = new Destination
                {
                    DestinationId=1,
                    Country = "Indonesia",
                    Description = "EcoTourism at its best in exquisite Bali",
                    Name = "Bali"
                };
                var lodging = new Lodging
                {
                    LodgingId=1,
                    Owner="Jshon",
                    Name = "Top Notch Resort and Spa",
                    MilesFromNearestAirport = 30,
                    IsResort=true,
                    Destination=destination
                };
                context.Lodgings.Add(lodging);
                context.SaveChanges();
            }

通過VS連接Oracle可以看到數據插入成功。

DB First

DB First顧名思義就是在先建好數據庫,再進行編程。我們新建一個項目,就以剛剛生成的那再張表來編程。

在新建項目中添加一個“ADO.NET 實體數據模型”:DBModel.edmx,選擇“從數據庫生成”

設置好連接串

選擇表

點擊完成,就會生成相應的模型。

我們來檢索一下剛剛插入的數據。

View Code
 using (Entities context = new Entities())
            {
                var des = context.Destinations.FirstOrDefault();
                var log = context.Lodgings.FirstOrDefault();
                Console.WriteLine("Lodging  Name:" + log.Name + " Owner:" + log.Owner);
                Console.WriteLine("Destination   Name:" + des.Name + " Country:" + des.Country);
            }

結果如圖。

PS:在DB First模式中,更要將Model First中所說的映射配置文件加入App.config中,不然很多數據類型映射會出錯。

 

偽Code First

見我的另一篇博客:Entity Framework Code First在Oracle下的偽實現

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved