淘宝网站页面设计,中山vi设计公司,网站当前位置 样式,潜江资讯网官网虽然EF6都快要出来了#xff0c;但是对于Oracle数据库#xff0c;仍然只能用DB first和Model First来编程#xff0c;不能用Code First真是一个很大的遗憾啊。 好了#xff0c;废话少说#xff0c;我们来看看EF中是如何用DB first和Model First来对Oracle编程的。 首先我们… 虽然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 HashSetLodging();}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 ICollectionLodging 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.clientsettingsadd namebool valueedmmapping number(1,0) /add namebyte valueedmmapping number(3,0) /add nameint16 valueedmmapping number(4,0) /add nameint32 valueedmmapping number(9,0) /add nameint64 valueedmmapping 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 DestinationIdin table Destinations
ALTER TABLE GYOUNG.Destinations
ADD CONSTRAINT PK_DestinationsPRIMARY KEY (DestinationId )ENABLEVALIDATE;-- Creating primary key on LodgingIdin table Lodgings
ALTER TABLE GYOUNG.Lodgings
ADD CONSTRAINT PK_LodgingsPRIMARY KEY (LodgingId )ENABLEVALIDATE;-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- ---------------------------------------------------- Creating foreign key on DestinationDestinationId in table Lodgings
ALTER TABLE GYOUNG.Lodgings
ADD CONSTRAINT FK_DestinationLodgingFOREIGN KEY (DestinationDestinationId)REFERENCES GYOUNG.Destinations(DestinationId)ENABLEVALIDATE;-- 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{DestinationId1,Country Indonesia,Description EcoTourism at its best in exquisite Bali,Name Bali};var lodging new Lodging{LodgingId1,OwnerJshon,Name Top Notch Resort and Spa,MilesFromNearestAirport 30,IsResorttrue,Destinationdestination};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下的伪实现 转载于:https://www.cnblogs.com/Gyoung/archive/2013/02/04/2881747.html