一直没有在项目里用过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有什么用处!?
希望大侠们可以帮我解惑!!!