不需要的NHibernate更新命令 [英] Unwanted NHibernate update command

查看:137
本文介绍了不需要的NHibernate更新命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下班级:

$ $ p $ public class Contact
{
public Contact(){
地址=新列表< Address>();
EmailAddresses = new List< EmailAddress>();
PhoneNumbers =新列表< PhoneNumber>();
}
public virtual int ContactID {get;私人设置}
公共虚拟公司{get;组; }
public virtual ContactType ContactType {get;组; }
公共虚拟字符串FullName {get;组; }
公共虚拟字符串FiscalCode {get;组; }
公共虚拟字符串注意{get;组; }
public virtual ContactRole ContactRole {get;组; }

公共虚拟IList<地址>地址{get;私人设置}
公共虚拟IList< EmailAddress> EmailAddresses {get;私人设置}
公共虚拟IList< PhoneNumber> PhoneNumbers {get;私人设置}
}

public class Address
{
public virtual int AddressID {get;私人设置}
公共虚拟字符串StreetAddress {get;组; }
公共虚拟字符串ZipCode {get;组; }
public virtual string City {get;组; }
public virtual Province Province {get;组; }
public virtual Country Country {get;组; }
public virtual联系联系{get;组; }
public virtual AddressType AddressType {get;组; }
public virtual bool PostalAddress {get;组; }
}

使用FluentNHibernate将这些类映射到数据库。这些是映射类

  public ContactMap(){
Table(Contacts);
Id(c => c.ContactID).Column(ContactID).GeneratedBy.Identity();
参考文献(c => c.Firm).Column(FirmID);
引用(c => c.ContactType).Column(ContactTypeID);
Map(c => c.FullName);
Map(c => c.FiscalCode);
Map(c => c.Notes);
引用(c => c.ContactRole).Column(ContactRoleID);
HasMany(c => c.Addresses).Cascade.SaveUpdate();
HasMany(c => c.EmailAddresses).Cascade.SaveUpdate();
HasMany(c => c.PhoneNumbers).Cascade.SaveUpdate();


$ b public AddressMap(){
表(地址);
Id(a => a.AddressID).Column(AddressID).GeneratedBy.Identity();
Map(a => a.StreetAddress);
Map(a => a.ZipCode);
地图(a => a.City);
引用(a => a.Province).Column(ProvinceID);
引用(a => a.Country).Column(CountryID);
参考(a => a.Contact).Column(ContactID);
引用(a => a.AddressType).Column(AddressTypeID);
Map(a => a.PostalAddress);

$



我试图使用这些类在数据库中加载大量的联系人。我的代码创建这些对象可以在逻辑上解释如下:

pre $创建联系人
对于此联系人的每个地址
创建地址
设置联系地址
将地址添加到联系人集合
下一个
对于此联系人的每个电子邮件
创建一个电子邮件
设置联系人电子邮件
将电子邮件添加到联系人集合
下一个

我没有任何问题,像电子邮件,PhoneNumber所有的后代集合,除了我有地址问题。事实上,当我尝试插入至少有一个地址的联系人时,我收到以下错误:

pre $ 无法插入集合: GSLConverter.Entities.Contact.Addresses#3551] [SQL:UPDATE地址SET AuthorID = @ p0 WHERE AddressID = @ p1]
无效的列名'AuthorID'

使用AuthorID而不是使用ContactID。 AuthorID来自哪里



这些是NHibernate在服务器上执行的查询


INSERT INTO地址(StreetAddress,ZipCode,City,PostalAddress,ProvinceID,CountryID,ContactID,AddressTypeID)VALUES(@ p0,@ p1,@ p2,@ p3, @ p4,@ p5,@ p6,@ p7);
选择SCOPE_IDENTITY();
@ p0 ='xxx xxxxxx,69',@ p1 ='80142',@ p2 ='xxxxxx',@ p3 = False,@ p4 = 1,@ p5 = 113,@ p6 = 3632,@ p7 = 1

UPDATE地址SET AuthorID = @ p0 WHERE AddressID = @ p1;
@ p0 = 3632,@ p1 = 26

@Stefan Steinegger:this是唯一一个引用AuthorID成员的类的映射

  public IssueNoteMap(){
表(IssueNotes);
Id(ino => ino.IssueNoteID).Column(IssueNoteID).GeneratedBy.Identity();
引用(ino => ino.Issue).Column(IssueID);
Map(ino => ino.NoteDate);
Map(ino => ino.NoteTitle);
Map(ino => ino.NoteBody);
引用(ino => ino.Author).Column(AuthorID);


$


AuthorID字段是对尚未映射的Contact表的引用在联系方面(正如你可以从前面的联系人映射中看到的那样)

解决方案

IssueNote.Author 任何一种类型的联系?如果是这样, Fluent映射 of

 参考资料(ino => ino.Author).Column(AuthorID); 

正在尝试更新您的地址表格,以确保其中还包含 ContactId AuthorId 的外键,当您映射到时,您已经告诉它 IssueNote.Author 联系 s



您已经告诉nHibernate:



当你真的想要这个:



因此,请将IssueNoteMap更改为以下内容,以便ContactId用于映射作者,并且它应该可以工作

 参考文献(ino => ino.Author).Column(ContactId); 


I have the following classes

public class Contact
{
    public Contact() {
        Addresses = new List<Address>();
        EmailAddresses = new List<EmailAddress>();
        PhoneNumbers = new List<PhoneNumber>();
    }
    public virtual int ContactID { get; private set; }
    public virtual Firm Firm { get; set; }
    public virtual ContactType ContactType { get; set; }
    public virtual string FullName { get; set; }
    public virtual string FiscalCode { get; set; }
    public virtual string Notes { get; set; }
    public virtual ContactRole ContactRole { get; set; }

    public virtual IList<Address> Addresses { get; private set; }
    public virtual IList<EmailAddress> EmailAddresses { get; private set; }
    public virtual IList<PhoneNumber> PhoneNumbers { get; private set; }
}

public class Address
{
    public virtual int AddressID { get; private set; }
    public virtual string StreetAddress { get; set; }
    public virtual string ZipCode { get; set; }
    public virtual string City { get; set; }
    public virtual Province Province { get; set; }
    public virtual Country Country { get; set; }
    public virtual Contact Contact { get; set; }
    public virtual AddressType AddressType { get; set; }
    public virtual bool PostalAddress { get; set; }
}

These classes have been mapped to the database using FluentNHibernate. These are the mapping classes

public ContactMap() {
    Table( "Contacts" );
    Id( c => c.ContactID ).Column( "ContactID" ).GeneratedBy.Identity();
    References( c => c.Firm ).Column( "FirmID" );
    References( c => c.ContactType ).Column( "ContactTypeID" );
    Map( c => c.FullName );
    Map( c => c.FiscalCode );
    Map( c => c.Notes );
    References( c => c.ContactRole ).Column( "ContactRoleID" );
    HasMany( c => c.Addresses ).Cascade.SaveUpdate();
    HasMany( c => c.EmailAddresses ).Cascade.SaveUpdate();
    HasMany( c => c.PhoneNumbers ).Cascade.SaveUpdate();
}


public AddressMap() {
    Table( "Addresses" );
    Id( a => a.AddressID ).Column( "AddressID" ).GeneratedBy.Identity();
    Map( a => a.StreetAddress );
    Map( a => a.ZipCode );
    Map( a => a.City );
    References( a => a.Province ).Column( "ProvinceID" );
    References( a => a.Country ).Column( "CountryID" );
    References( a => a.Contact ).Column( "ContactID" );
    References( a => a.AddressType ).Column( "AddressTypeID" );
    Map( a => a.PostalAddress );
}

I am trying to load a considerable amount of contacts inside the database using these classes. My code that create these object can be logically explained as follow

Create a contact
For each address of this contact 
    create an address
    set the contact address
    add the address to the contact collection
Next
For each email of this contact 
    create an email
    set the contact email
    add the email to the contact collection
Next

I have no problem with all the descendant collection like Email, PhoneNumber except that I have a problem with Address. In fact when I try to insert an contact that has at least one address I get the following error

Could not insert collection: [GSLConverter.Entities.Contact.Addresses#3551][SQL: UPDATE Addresses SET AuthorID = @p0 WHERE AddressID = @p1]
Invalid column name 'AuthorID'

Instead of using ContactID is using AuthorID. Where does that AuthorID come from????

These are the queries that NHibernate execute on the server

INSERT INTO Addresses (StreetAddress, ZipCode, City, PostalAddress, ProvinceID, CountryID, ContactID, AddressTypeID) VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7); 
select SCOPE_IDENTITY();
@p0 = 'xxx xxxxxx, 69 ', @p1 = '80142', @p2 = 'xxxxxx', @p3 = False, @p4 = 1, @p5 = 113, @p6 = 3632, @p7 = 1

UPDATE Addresses SET AuthorID = @p0 WHERE AddressID = @p1;
@p0 = 3632, @p1 = 26

@Stefan Steinegger: this is the mapping of the class, the only one, that reference an AuthorID member

public IssueNoteMap() {
    Table( "IssueNotes" );
    Id( ino => ino.IssueNoteID ).Column( "IssueNoteID" ).GeneratedBy.Identity();
    References( ino => ino.Issue ).Column( "IssueID" );
    Map( ino => ino.NoteDate );
    Map( ino => ino.NoteTitle );
    Map( ino => ino.NoteBody );
    References( ino => ino.Author ).Column( "AuthorID" );
}

The AuthorID field is a reference to the Contact table that have not be still mapped on the Contact side (as you can see from the previous Contact mapping

解决方案

Is IssueNote.Author by any chance a type of Contact? If so, the Fluent Mapping of

References( ino => ino.Author ).Column( "AuthorID" );

is attempting to update your 'Addresses' table to ensure that it also has the ContactIdstored in the Foreign Key of AuthorId which you have told it that IssueNote.Author uses when mapping to Contacts

You have told nHibernate this:

When you really want this:

So, change the IssueNoteMap to the following so that the ContactId is used to map the Author and it should work

References( ino => ino.Author ).Column( "ContactId" );

这篇关于不需要的NHibernate更新命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆