流利的NHibernate添加和更新问题:参考 [英] Fluent NHibernate Adding and Updating problem : References

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

问题描述

要流畅地进入休眠状态,我还挺n00bish的,但是我的一个存储库中有一个意外错误.

Im fairly n00bish when it comes to fluent nhibernate but i have an unexpected error in one of my repositories.

我的数据类型为CostCode

I have a datatype CostCode

public class CostCode
{
    public virtual int Id { get; set; }
    public virtual String CostCodeCode { get; set; }
    public virtual Company Company { get; set; }
    public virtual DateTime CreatedDate { get; set; }
    public virtual String CreatedBy { get; set; }
    public virtual DateTime ModifiedDate { get; set; }
    public virtual String ModifiedBy { get; set; }
}

这是映射

public sealed class CostCodeMap : ClassMap<CostCode>
{
    /**
     * @breif Mapping Constructor
     */

    public CostCodeMap()
    {
        Id(Reveal.Member<CostCode>("Id"));
        Map(x => x.CostCodeCode).Not.Nullable();
        References(x => x.Company, "CompanyId").Cascade.All();
        Map(x => x.CreatedDate).Not.Nullable();
        Map(x => x.CreatedBy).Not.Nullable();
        Map(x => x.ModifiedDate).Not.Nullable();
        Map(x => x.ModifiedBy).Not.Nullable();
    }
}

当我尝试对此进行更新时,出现错误"Domain.DataTypes.Company实例的标识符从1更改为8"

When i try to update this, i get an error "identifier of an instance of Domain.DataTypes.Company was altered from 1 to 8"

现在,我认为这是我设置映射的方式,可能还有我的存储库如何处理更新/添加.

Now i think its the way that i set up the mapping, and possibly how my repository is handling the updates/adds.

我有一个控制公司ID的下拉列表,在即时消息添加/更新时,我将物业公司设置为数据库中已更新为ID的任何内容.

I have a drop down list that controls the id of the company, and when im adding/updating i set the property company to whatever is in the database for the id that it has been updated to.

var companyRepository= new CompanyRepository(_session);
temp.Company = companyRepository.GetCompanyById(temp.Company.Id);

_session.Update(c);                

有人可以给我一个提示/解决方案来帮助我吗?通过查看相关问题,问题可能出在任何地方.

Can anyone give me a hint/solution to help me on my way? Looking through related problems here, the problem could be anything.

推荐答案

好吧,我会把它扔掉...我敢打赌,正在发生的事情是您通过更改ID来设置temp.Company.Id,然后使用要使用更改后的ID获取该公司的仓库. NHibernate将跟踪您在另一家公司上更改了ID的情况.使用临时变量存储该新公司ID,请勿更改其他公司的ID.

Ok, I will just throw this out... I bet what is happening is you are setting temp.Company.Id by changing the Id, then you use the repo to go fetch that company using the changed Id. NHibernate will track that you changed the Id on the other company however. Use a temp var to store that new company id, dont change the id of the other company.

这篇关于流利的NHibernate添加和更新问题:参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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