一直沒有在項目里用過NHibernate3.2的CodeFirst 這次因為項目需要用到Oracle數據庫而我對EF對Oracle支持心里犯嘀咕所以為保險起見我用NHibernate,以前都是寫就映射文件的,現在3.2里已經集成了Maping Bycode工具可以說是NHibernate對CodeFirst支持得很不錯了!我也不想采用Xml文件了(至於原因大家都懂得的!)
明白了大多數的NHibernate映射參數采用CodeFist也應該不是什么難事!
所以今天就試了一把,以前也試過但只粗略的試了一下!出現了以下問題,(結果谷爹 度娘 一起用也沒查到個所以然!只是找了一堆EFCodeFirst的東西,NHibernate的幾乎很少)小弟真不知道是怎么回事兒,所以特向園子里的大俠請教!
廢話不多說了,先上代碼:
public class Student { public virtual int StudentID { set; get; } public virtual string StudentName { set; get; } public virtual IList<CurrClass> CurrClasses { set; get; } } public class CurrClass { public virtual int ClassID { set; get; } public virtual int ClassName { set; get; } public virtual IList<Student> Students { set; get; } } public class CurrClassMap : ClassMapping<CurrClass> { public CurrClassMap() { this.Id(p => p.ClassID, map => { map.Generator(Generators.Native); }); this.Property(p => p.ClassName); this.Bag(p => p.Students, map => { map.Table("Student_Class"); }); } } public class StudentMap : ClassMapping<Student> { public StudentMap() { this.Id(p => p.StudentID, map => { map.Generator(Generators.Native); }); this.Property(p => p.StudentName, map => { map.Length(500); }); this.Bag(p => p.CurrClasses, map => { map.Table("Student_Class"); }); } }
用過的朋友一看就知道是個多對多的關系!看似沒什么問題,那么我們來看下生成映射文件是不是夠標准:
<?xml version="1.0" encoding="utf-8"?> <hibernate-mapping xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" namespace="DALTestCase.domain" assembly="DALTestCase" xmlns="urn:nhibernate-mapping-2.2"> <class name="CurrClass"> <id name="ClassID" type="Int32"> <generator class="native" /> </id> <property name="ClassName" /> <bag name="Students" table="Student_Class"> <key column="currclass_key" /> <many-to-many class="Student" /> </bag> </class> <class name="Student"> <id name="StudentID" type="Int32"> <generator class="native" /> </id> <property name="StudentName" length="500" /> <bag name="CurrClasses" table="Student_Class"> <key column="student_key" /> <many-to-many class="CurrClass" /> </bag> </class> </hibernate-mapping>
很不錯!和手寫的沒兩樣!
再來看看我們生成的數據庫腳本(我這里是用的Sql2005):
if exists (select 1 from sys.objects where object_id = OBJECT_ID(N'[FKCF48CF07D9A53447]') AND parent_object_id = OBJECT_ID('Student_Class')) alter table Student_Class drop constraint FKCF48CF07D9A53447 if exists (select 1 from sys.objects where object_id = OBJECT_ID(N'[FKCF48CF07401A93]') AND parent_object_id = OBJECT_ID('Student_Class')) alter table Student_Class drop constraint FKCF48CF07401A93 if exists (select 1 from sys.objects where object_id = OBJECT_ID(N'[FKCF48CF077158C5E0]') AND parent_object_id = OBJECT_ID('Student_Class')) alter table Student_Class drop constraint FKCF48CF077158C5E0 if exists (select * from dbo.sysobjects where id = object_id(N'CurrClass') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table CurrClass if exists (select * from dbo.sysobjects where id = object_id(N'Student_Class') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table Student_Class if exists (select * from dbo.sysobjects where id = object_id(N'Student') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table Student create table CurrClass ( ClassID INT IDENTITY NOT NULL, ClassName INT null, primary key (ClassID) ) create table Student_Class ( currclass_key INT not null, elt INT not null,///這里就是我說的莫名奇妙的多出來了一個字段 student_key INT not null ) create table Student ( StudentID INT IDENTITY NOT NULL, StudentName NVARCHAR(500) null, primary key (StudentID) ) alter table Student_Class add constraint FKCF48CF07D9A53447 foreign key (elt) references Student alter table Student_Class add constraint FKCF48CF07401A93 foreign key (currclass_key) references CurrClass alter table Student_Class add constraint FKCF48CF077158C5E0 foreign key (student_key) references Student
正在我看的時侯突然發現,第三個表里竟然多了一個elt的字段,到底是怎么回事兒呢!?現在我也沒鬧明白!怎么會憑空多個字段出來!?
結果表結構就變成了下面的這個樣子:
至於生成的涉及Student_Class的這個表生成的SQL語句都會存在這個elt的字段。如果操作Student_Class表生成的SQL語句會出現不能執行的錯誤!
為什么會出現elt的字段?為什么用Xml就不會出現elt的字段?這個elt有什么用處!?
希望大俠們可以幫我解惑!!!