在做SchemaExport.Execute测试的时候发现在Order.hbm.xml中明明已经设置了的“级联删除”属性,但是却没有在生成的数据库中有任何作用。我的设置如下:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="DomainModel" namespace="DomainModel">
<class name="DomainModel.Entities.Order,DomainModel" table="[Order]" >
<id name="OrderId" column="OrderId" type="Int32" unsaved-value="0">
<generator class="native" />
</id>
<property name="OrderDate" column="OrderDate" type="DateTime"
not-null="true" />
<!--多对一关系:Orders属于一个Customer-->
<many-to-one cascade="all" name="Customer" column="Customer" not-null="true"
class="DomainModel.Entities.Customer,DomainModel"
foreign-key="FK_CustomerOrders" />
<!--多对多关系:Order有多个Products-->
<bag name="Products" cascade="all" generic="true" table="OrderProduct">
<key column="[Order]" foreign-key="FK_OrderProducts"/>
<many-to-many column="Product"
class ="DomainModel.Entities.Product,DomainModel"
foreign-key="FK_ProductOrders"/>
</bag>
</class>
</hibernate-mapping>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="DomainModel" namespace="DomainModel">
<class name="DomainModel.Entities.Order,DomainModel" table="[Order]" >
<id name="OrderId" column="OrderId" type="Int32" unsaved-value="0">
<generator class="native" />
</id>
<property name="OrderDate" column="OrderDate" type="DateTime"
not-null="true" />
<!--多对一关系:Orders属于一个Customer-->
<many-to-one cascade="all" name="Customer" column="Customer" not-null="true"
class="DomainModel.Entities.Customer,DomainModel"
foreign-key="FK_CustomerOrders" />
<!--多对多关系:Order有多个Products-->
<bag name="Products" cascade="all" generic="true" table="OrderProduct">
<key column="[Order]" foreign-key="FK_OrderProducts"/>
<many-to-many column="Product"
class ="DomainModel.Entities.Product,DomainModel"
foreign-key="FK_ProductOrders"/>
</bag>
</class>
</hibernate-mapping>
请看黄色背景色标注的地方,明明已经设置了 cascade="all"
但是却没有起作用。另外在2.1.0版本中解决李永京所说的SchemaUpdate类不能更新字段长度的问题,但是非空还是没有做到更新,只能期待下一个版本了。
下面是SchemaUpdate生成的SQL
------ Test started: Assembly: DALTest.dll ------
if exists (select 1 from sys.objects where object_id = OBJECT_ID(N'[FK_CustomerOrders]') AND parent_object_id = OBJECT_ID('[Order]'))
alter table [Order] drop constraint FK_CustomerOrders
if exists (select 1 from sys.objects where object_id = OBJECT_ID(N'[FK_ProductOrders]') AND parent_object_id = OBJECT_ID('OrderProduct'))
alter table OrderProduct drop constraint FK_ProductOrders
if exists (select 1 from sys.objects where object_id = OBJECT_ID(N'[FK_OrderProducts]') AND parent_object_id = OBJECT_ID('OrderProduct'))
alter table OrderProduct drop constraint FK_OrderProducts
if exists (select * from dbo.sysobjects where id = object_id(N'[Order]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [Order]
if exists (select * from dbo.sysobjects where id = object_id(N'OrderProduct') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table OrderProduct
if exists (select * from dbo.sysobjects where id = object_id(N'Customer') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table Customer
if exists (select * from dbo.sysobjects where id = object_id(N'viewCustomer') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table viewCustomer
if exists (select * from dbo.sysobjects where id = object_id(N'Product') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table Product
create table [Order] (
OrderId INT IDENTITY NOT NULL,
OrderDate DATETIME not null,
Customer INT not null,
primary key (OrderId)
)
create table OrderProduct (
[Order] INT not null,
Product INT not null
)
create table Customer (
CustomerId INT IDENTITY NOT NULL,
Version INT not null,
Firstname NVARCHAR(50) null,
Lastname NVARCHAR(50) null,
primary key (CustomerId),
unique (Firstname, Lastname)
)
create table viewCustomer (
CustomerId INT IDENTITY NOT NULL,
Firstname NVARCHAR(255) null,
Lastname NVARCHAR(255) null,
OrderId INT null,
OrderDate DATETIME null,
primary key (CustomerId)
)
create table Product (
ProductId INT IDENTITY NOT NULL,
Name NVARCHAR(50) not null,
Cost REAL not null,
primary key (ProductId)
)
alter table [Order]
add constraint FK_CustomerOrders
foreign key (Customer)
references Customer
alter table OrderProduct
add constraint FK_ProductOrders
foreign key (Product)
references Product
alter table OrderProduct
add constraint FK_OrderProducts
foreign key ([Order])
references [Order]
1 passed, 0 failed, 0 skipped, took 13.13 seconds (NUnit 2.5.1).
本文部分内容摘引自: YJingLee's Blog
if exists (select 1 from sys.objects where object_id = OBJECT_ID(N'[FK_CustomerOrders]') AND parent_object_id = OBJECT_ID('[Order]'))
alter table [Order] drop constraint FK_CustomerOrders
if exists (select 1 from sys.objects where object_id = OBJECT_ID(N'[FK_ProductOrders]') AND parent_object_id = OBJECT_ID('OrderProduct'))
alter table OrderProduct drop constraint FK_ProductOrders
if exists (select 1 from sys.objects where object_id = OBJECT_ID(N'[FK_OrderProducts]') AND parent_object_id = OBJECT_ID('OrderProduct'))
alter table OrderProduct drop constraint FK_OrderProducts
if exists (select * from dbo.sysobjects where id = object_id(N'[Order]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [Order]
if exists (select * from dbo.sysobjects where id = object_id(N'OrderProduct') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table OrderProduct
if exists (select * from dbo.sysobjects where id = object_id(N'Customer') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table Customer
if exists (select * from dbo.sysobjects where id = object_id(N'viewCustomer') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table viewCustomer
if exists (select * from dbo.sysobjects where id = object_id(N'Product') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table Product
create table [Order] (
OrderId INT IDENTITY NOT NULL,
OrderDate DATETIME not null,
Customer INT not null,
primary key (OrderId)
)
create table OrderProduct (
[Order] INT not null,
Product INT not null
)
create table Customer (
CustomerId INT IDENTITY NOT NULL,
Version INT not null,
Firstname NVARCHAR(50) null,
Lastname NVARCHAR(50) null,
primary key (CustomerId),
unique (Firstname, Lastname)
)
create table viewCustomer (
CustomerId INT IDENTITY NOT NULL,
Firstname NVARCHAR(255) null,
Lastname NVARCHAR(255) null,
OrderId INT null,
OrderDate DATETIME null,
primary key (CustomerId)
)
create table Product (
ProductId INT IDENTITY NOT NULL,
Name NVARCHAR(50) not null,
Cost REAL not null,
primary key (ProductId)
)
alter table [Order]
add constraint FK_CustomerOrders
foreign key (Customer)
references Customer
alter table OrderProduct
add constraint FK_ProductOrders
foreign key (Product)
references Product
alter table OrderProduct
add constraint FK_OrderProducts
foreign key ([Order])
references [Order]
1 passed, 0 failed, 0 skipped, took 13.13 seconds (NUnit 2.5.1).