Asp.net 使用NHibernate基于外键在子表中插入行
我希望有人以前遇到过这个问题,可以帮助我解决。在我的数据库中,我使用了表之间的关系Asp.net 使用NHibernate基于外键在子表中插入行,asp.net,nhibernate,nhibernate-mapping,Asp.net,Nhibernate,Nhibernate Mapping,我希望有人以前遇到过这个问题,可以帮助我解决。在我的数据库中,我使用了表之间的关系 Ex:Authors,Author Books,Booksstore(salesDetails) **Authors table** ================= Aid int (Identity and Primarykey),AuthorName varchar(50) ================= *********** **Author Books table** ============
Ex:Authors,Author Books,Booksstore(salesDetails)
**Authors table**
=================
Aid int (Identity and Primarykey),AuthorName varchar(50)
=================
***********
**Author Books table**
=================
BookID int(primary key),BookName nvarchar(50),Aid int (foreign key Authorstable(Aid))
=================
**************
**Booksstore table**
================
StoreID int(primary key),Totalsales int,BookID int (foreign key Author Bookstable(BookID))
================
I'm using Nhibernate and mapping above tables in the following
*********************
**Authorsdao.hdm.xml**
*********************
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Dal" namespace="Dal">
<class name="AurthosDao,Dal" table="Aurthors Table" lazy="true">
<id name="AId" column="AuthorID" type="int">
<generator class="native" />
</id>
<property type="string" not-null="true" length="250" name="Authorname" column="AuthorName" />
</class>
</hibernate-mapping>
*********************
**Authorbooksdao.hdm.xml**
*********************
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Dal" namespace="Dal">
<class name="AurthorBooksDao,Dal" table="AuthorBookstable" lazy="true">
<id name="AId" column="AuthorID">
<generator class="foreign">
<param name="property">AId</param>
</generator>
</id>
<property type="int" not-null="true" name="BookId" column="BookID" />
<property type="string" not-null="true" name="Bookname" column="BookName" />
</class>
</hibernate-mapping>
*********************
**Booksstoredao.hdm.xml**
*********************
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Dal" namespace="Dal">
<class name="BookStoreDao,Dal" table="Bookstoretable" lazy="true">
<id name="BookId" column="BookID">
<generator class="foreign">
<param name="property">BookId</param>
</generator>
</id>
<property type="int" not-null="true" name="StoreId" column="StoreID" />
<property type="int" not-null="true" name="Totalsales" column="TotalSales" />
</class>
</hibernate-mapping>
***************************
Now I have to insert the data from Asp.Net Nhibernate in to 3 tables in one transaction.
1)I'm inserting first in Authors table after that I'm trying to get that last inserting key from Authors table and pushing into Authorsbook table
Ex:作者、作者书籍、书店(销售详情)
**作者表**
=================
Aid int(标识和主密钥),AuthorName varchar(50)
=================
***********
**作者图书表**
=================
BookID int(主键)、BookName nvarchar(50)、Aid int(外键Authorstable(Aid))
=================
**************
**书店桌子**
================
StoreID int(主键)、Totalsales int、BookID int(外键Author Bookstable(BookID))
================
我在下面的表格中使用了Nhibernate和上面的映射
*********************
**Authorsdao.hdm.xml**
*********************
*********************
**Authorbooksdao.hdm.xml**
*********************
帮助
*********************
**bookstoredao.hdm.xml**
*********************
书呆子
***************************
现在我必须在一个事务中将Asp.NETNHibernate中的数据插入到3个表中。
1) 我首先在Authors表中插入,然后我尝试从Authors表中获取最后一个插入键,并将其推入Authorsbook表
2) 插入到authorsBooks表后,尝试从AuthorsBook表获取最后一个插入的BookID,并将其推入书店表
如果有什么帮助的话,那将是值得感激的
Radmin我在以下公共IAuthors Aurthorsdata中使用Transaction(PlanningManagerProxy代理、字符串authorName、IBook books、Ilist、Ilist属性)
{
AuthorDao dao=新的AuthorDao()
使用(ITransaction tx=proxy.MarcomManager.GetTransaction())
{
//AuthorObjective的业务逻辑
dao.Name=authorName;
tx.PersistenceManager.PlanningRepository.Save(dao);
tx.Commit();
authordaoobjlist;
使用(ITransaction txGet=proxy.GetTransaction())
{
objList=txGet.PersistenceManager.PlanningRepository.Get(id);
txGet.Commit();
}
var objId=来自对象列表中的,其中a.AuthorID==AuthorID选择a;
使用(ITransaction tx2=proxy.MarcomManager.GetTransaction())
{
AuthorbooksDao objdao=新的AuthorbooksDao();
objdao.BookId=books.BookId;
objdao.BookName=books.BookName;
objdao.AuthorId=objId.AuthorId;
tx2.PersistenceManager.PlanningRepository.Save(objdao);
提交();
}
}
}
@Radim首先我要提交Authors事务,然后从AuthorDao(AuthorsTable)获取最后一条插入的记录,然后推送到Bookstore表。但是在映射时间时,外键不允许插入?让我们尝试这种映射(将表声明作为常量)
C类:
xml映射:
<class name="Author" table="Authors" lazy="true">
<id name="ID" column="Aid" type="int">
<generator class="native" />
</id>
<property type="string" not-null="true" length="250" name="Authorname" column="AuthorName" />
</class>
<class name="Book" table="AuthorBooks" lazy="true">
<id name="ID" column="BookID">
<generator class="native" />
</id>
<!-- reference Author -->
<many-to-one name="Author" column="AID" cascade="all" />
<property type="string" not-null="true" name="Bookname" column="BookName" />
</class>
<class name="BookStore" table="Bookstore" lazy="true">
<id name="ID" column="StoreID">
<generator class="native" />
</id>
<!-- reference Book -->
<many-to-one name="Book" column="BookID" cascade="all" />
<property type="int" not-null="true" name="Totalsales" column="TotalSales" />
</class>
注意:在你的代码片段中有一些不正确的地方,所以唯一的方法就是在绿色区域显示一个例子。例如,该文件应命名为…hbm.xml(而不是…hdm.xml,AurthosDao而不是AuthorsDao等)感谢您的精彩解释和代码。但我试图引用类名“Author”在到书店的参考资料中获取运行时映射错误。我可以知道我在哪里犯了错误吗?请你扩展你的问题,并显示你的C#entity类,以及错误的内容stacktrace?然后我们可以做进一步的步骤!;)Radmin我编辑了上面的帖子,你能指导一下吗?很难说,出了什么问题,因为你的地图太复杂了。尝试更改
aurstorbooksdao
以遵循我的Book
映射。
应用于一对一映射,请参见:。虽然Author Books表有主键(BookId),但应将其映射为,问题片段中显示的方法是过于复杂。如果可能,尝试更改映射以使用更多ORM标准样式(映射引用,使用表主键作为ID…)。因为现在这是我能给你的唯一建议。。。
public class Author
{
public virtual int ID { get; set; }
public virtual string Authorname { get; set; }
}
public class Book
{
public virtual int ID { get; set; }
public virtual Author Author { get; set; }
public virtual string Bookname { get; set; }
}
public class BookStore
{
public virtual int ID { get; set; }
public virtual Book Book { get; set; }
}
<class name="Author" table="Authors" lazy="true">
<id name="ID" column="Aid" type="int">
<generator class="native" />
</id>
<property type="string" not-null="true" length="250" name="Authorname" column="AuthorName" />
</class>
<class name="Book" table="AuthorBooks" lazy="true">
<id name="ID" column="BookID">
<generator class="native" />
</id>
<!-- reference Author -->
<many-to-one name="Author" column="AID" cascade="all" />
<property type="string" not-null="true" name="Bookname" column="BookName" />
</class>
<class name="BookStore" table="Bookstore" lazy="true">
<id name="ID" column="StoreID">
<generator class="native" />
</id>
<!-- reference Book -->
<many-to-one name="Book" column="BookID" cascade="all" />
<property type="int" not-null="true" name="Totalsales" column="TotalSales" />
</class>
// instances creation
var author = new Author { Authorname = "my author" };
var book = new Book { Bookname = "my book "};
var store = new BookStore { TotalSales = 1 };
// references
book.Author = author;
store.Book = book;
// cascade all will store them all with correct ID
session.Save(author);