Nhibernate 如何映射遗留数据库中的表继承?

Nhibernate 如何映射遗留数据库中的表继承?,nhibernate,fluent-nhibernate,Nhibernate,Fluent Nhibernate,我发现很难映射遗留数据库 下面是一个场景:一个承包商申请一个新的许可证。将查询并显示许可证。承包商修改任何需要修改的信息。完成后,应用程序将保存到数据库中 许可属于企业或所有者,这是任何多态关联的许可。许可证本身包含用于建立关联的TableID和RecordID 许可证有许多应用程序。此系统中有几种类型的应用程序,因此有一个“ApplicationCommon”表和一个“LicenseApplication”表。此特定应用程序是一个许可证的应用程序。ApplicationCommon和Licen

我发现很难映射遗留数据库

下面是一个场景:一个承包商申请一个新的许可证。将查询并显示许可证。承包商修改任何需要修改的信息。完成后,应用程序将保存到数据库中

许可属于企业或所有者,这是任何多态关联的许可。许可证本身包含用于建立关联的TableID和RecordID

许可证有许多应用程序。此系统中有几种类型的应用程序,因此有一个“ApplicationCommon”表和一个“LicenseApplication”表。此特定应用程序是一个许可证的应用程序。ApplicationCommon和LicenseApplication共享相同的主键。换句话说,ApplicationCommon id和LicenseApplication id是相同的。所以这就是表继承(或者我认为是这样)

由于与业务的奇怪多态性关联,我必须从许可证中单独查询业务数据(或者我认为是nHibernate的新手)。一旦我有了许可证和业务,我就会尝试创建一个新的应用程序,也就是当系统出现故障时

以下是我得到的一个例外:

 "object references an unsaved transient instance - save the transient instance 
 before flushing. Type: LicenseApplication, Entity: LicenseApplication"
以下是映射:

    public LicenseApplicationMap()
    {
        Table("Applications_LicenseApplication");
        Id(x => x.ID).Column("intApplicationID").Not.Nullable();
        Map(x => x.TableID).Column("intTableID").Nullable();
        Map(x => x.RecordID).Column("intRecordID").Nullable();
        References(x => x.Type).Column("intLicenseTypeID").Not.Nullable();

        Map(x => x.InsuranceExpiresOn).Column("dtInsuranceExpiresOn");
        Map(x => x.WCInsuranceExpiresOn).Column("dtWCInsuranceExpiresOn");
        Map(x => x.Updated).Column("dtUpdated");
        Map(x => x.AuthorizedRepresentativeFirstName).Column("strAuthRepFirstName");
        Map(x => x.AuthorizedRepresentativeLastName).Column("strAuthRepLastName");
        Map(x => x.AuthorizedRepresentativeGreeting).Column("strAuthRepGreeting");

        HasOne(x => x.ApplicationCommon).ForeignKey("intApplicationID").Cascade.All();
        References(x => x.License).Column("intLicenseID");

        HasMany(x => x.Officers).KeyColumn("intApplicationID");
        HasMany(x => x.Designees).KeyColumn("intApplicationID");
    }

最后,我使用的是.NETMVC、AutoMapper和S#rpArch,这是我的控制器操作和查询。SaveLicense和SaveQuery执行相同的逻辑,SaveOrUpdate,然后Flush

    [HttpPost]
    [Transaction]
    public ActionResult Edit(int id, LicenseViewModel applicationData)
    {
        // 1. Get the current license.
        var license = _licenseService.GetLicenseCommon(id);

        // 2. Make changes to license that where made during application process
        Mapper.Map<LicenseViewModel , LicenseCommon>(applicationData, license);
        var business = _licenseService.GetBusiness(license.RecordID);
        Mapper.Map<LicenseViewModel , Business>(applicationData, business);
        business.LastUpdated = DateTime.Now;

        // 3. Create a new application and add it to the license
        var application = new LicenseApplication();
        Mapper.Map<LicenseViewModel , LicenseApplication>(applicationData, application);
        application.Updated = DateTime.Now;
        application.InsuranceExpiresOn = DateTime.Parse(applicationData.GeneralLiabilityExpiration);
        application.WCInsuranceExpiresOn = DateTime.Parse(applicationData.WorkmansCompensationExpiration);
        application.TableID = 33;
        application.RecordID = license.RecordID;


        // 4. Save the license and it's applications to the database.
        license.Business = business;  //Don't think this works at all...
        license.Applications.Add(application);
        application.License = license;
        ///////////////// BOOM THIS IS WHERE IT BLOWS UP //////////////////////
        _licenseService.SaveLicense(license);
        _businessService.SaveBusiness(business);

        // 5. Convert the modified license and it's newly created application to an LicenseViewModel
        Mapper.Map<LicenseCommon, LicenseViewModel >(license, applicationData);

        // 6. return json or error message
        return Json(applicationData);
    }
[HttpPost]
[交易]
公共操作结果编辑(int id,LicenseViewModel应用程序数据)
{
//1.获取当前许可证。
var license=\u licenseService.getLicenseComon(id);
//2.在申请过程中对许可证进行更改
Mapper.Map(应用程序数据、许可证);
var business=\u licenseService.GetBusiness(license.RecordID);
Mapper.Map(应用数据、业务);
business.LastUpdated=DateTime.Now;
//3.创建新应用程序并将其添加到许可证中
var应用程序=新的许可证应用程序();
Map(applicationData,application);
application.Updated=DateTime.Now;
application.InsuranceExpiresOn=DateTime.Parse(applicationData.GeneralLiabilityExpiration);
application.WCInsuranceExpiresOn=DateTime.Parse(applicationData.WorkmansCompensationExpiration);
application.TableID=33;
application.RecordID=license.RecordID;
//4.将许可证及其应用程序保存到数据库中。
license.Business=Business;//我认为这根本没用。。。
license.Applications.Add(应用程序);
application.License=许可证;
/////////////////轰,这就是它爆炸的地方//////////////////////
_licenseService.SaveLicense(许可证);
_businessService.SaveBusiness(business);
//5.将修改后的许可证及其新创建的应用程序转换为LicenseViewModel
Mapper.Map(许可证、应用程序数据);
//6.返回json或错误消息
返回Json(applicationData);
}
请告诉我我的映射有什么问题。

有很多(x=>x.Applications)
缺少一个
.Cascade.All()
,因此保存许可证也可以保存对应用程序的更改

而且
.Inverse
也可以告诉NH应用程序负责关联

而且

// class LicenseCommon
public void Add(LicenseApplication application)
{
    application.License = license;
    application.TableID = 33;
    application.RecordID = license.RecordID;
    Applications.Add(application);
}

您可以使用
ReferenceAny(license=>license.ApplicatedTo).column(“intTableID”).Metavaluetype().Metavalue(33)
而不是保存
TableId
,但是,在我最初的尝试中,我尝试过这样做,我如何做
var businesses=Session.QueryOver()。其中(x=>x.Owner是Business)
<代码>x。所有者在linq工作。不幸的是,这就是我质疑它的方式。我获取许可证,然后使用linq获取我的业务许可证。这应该执行
Session.Query()。选择(x=>x.license).ToList()是的,我们有一个服务层来处理添加,我只是想做点事情。非常感谢你的帮助。
    public BusinessMap()
    {
        Table("Core_Business");
        Id(x => x.ID).Column("intBusinessID").GeneratedBy.Identity();

        Map(x => x.BusinessName).Column("strBusinessName").Not.Nullable();
        Map(x => x.PhoneNumber).Column("strPhoneNumber").Not.Nullable();
        Map(x => x.FaxNumber).Column("strFaxNumber").Not.Nullable();
        Map(x => x.EmailAddress).Column("strEmailAddress").Not.Nullable();
        Map(x => x.YearOrganized).Column("strYearOrganized").Not.Nullable();
        Map(x => x.LastUpdated).Column("dtLastUpdated").Not.Nullable();

        Map(x => x.PhysicalAddr1).Column("strPhysicalAddr1").Not.Nullable();
        Map(x => x.PhysicalAddr2).Column("strPhysicalAddr2").Nullable();
        Map(x => x.PhysicalCity).Column("strPhysicalCity").Not.Nullable();
        Map(x => x.PhysicalState).Column("strPhysicalState").Not.Nullable();
        Map(x => x.PhysicalCountry).Column("strPhysicalCountry").Not.Nullable();

        Map(x => x.PhysicalZip).Column("strPhysicalZip").Not.Nullable();

        Map(x => x.MailingAddr1).Column("strMailingAddr1").Not.Nullable();
        Map(x => x.MailingAddr2).Column("strMailingAddr2").Nullable();
        Map(x => x.MailingCity).Column("strMailingCity").Not.Nullable();
        Map(x => x.MailingState).Column("strMailingState").Not.Nullable();
        Map(x => x.MailingCountry).Column("strMailingCountry").Not.Nullable();

        Map(x => x.MailingZip).Column("strMailingZip").Not.Nullable();
    }
    [HttpPost]
    [Transaction]
    public ActionResult Edit(int id, LicenseViewModel applicationData)
    {
        // 1. Get the current license.
        var license = _licenseService.GetLicenseCommon(id);

        // 2. Make changes to license that where made during application process
        Mapper.Map<LicenseViewModel , LicenseCommon>(applicationData, license);
        var business = _licenseService.GetBusiness(license.RecordID);
        Mapper.Map<LicenseViewModel , Business>(applicationData, business);
        business.LastUpdated = DateTime.Now;

        // 3. Create a new application and add it to the license
        var application = new LicenseApplication();
        Mapper.Map<LicenseViewModel , LicenseApplication>(applicationData, application);
        application.Updated = DateTime.Now;
        application.InsuranceExpiresOn = DateTime.Parse(applicationData.GeneralLiabilityExpiration);
        application.WCInsuranceExpiresOn = DateTime.Parse(applicationData.WorkmansCompensationExpiration);
        application.TableID = 33;
        application.RecordID = license.RecordID;


        // 4. Save the license and it's applications to the database.
        license.Business = business;  //Don't think this works at all...
        license.Applications.Add(application);
        application.License = license;
        ///////////////// BOOM THIS IS WHERE IT BLOWS UP //////////////////////
        _licenseService.SaveLicense(license);
        _businessService.SaveBusiness(business);

        // 5. Convert the modified license and it's newly created application to an LicenseViewModel
        Mapper.Map<LicenseCommon, LicenseViewModel >(license, applicationData);

        // 6. return json or error message
        return Json(applicationData);
    }
// class LicenseCommon
public void Add(LicenseApplication application)
{
    application.License = license;
    application.TableID = 33;
    application.RecordID = license.RecordID;
    Applications.Add(application);
}