Fluent NHibernate 添加和更新问题:参考资料

发布于 2024-09-28 15:44:58 字数 1447 浏览 2 评论 0原文

当谈到流畅的 nhibernate 时,我相当不懂,但我的一个存储库中出现了意外错误。

我有一个数据类型 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”

现在我认为这是我设置的方式映射,以及我的存储库如何处理更新/添加。

我有一个控制公司 ID 的下拉列表,当我添加/更新时,我将房地产公司设置为数据库中已更新的 ID 中的任何内容。

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

_session.Update(c);                

谁能给我一个提示/解决方案来帮助我?在这里查看相关问题,问题可能是任何事情。

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

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; }
}

and here is the mapping

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();
    }
}

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扮仙女 2024-10-05 15:44:58

好吧,我就把这个扔掉......我敢打赌,发生的事情是您通过更改 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.

一场信仰旅途 2024-10-05 15:44:58

我不是 100% 确定,但看起来确实可能存在一个错误:

temp.Company = ...(temp.Company.Id);

我认为您实际上是从传入参数中提取它。

另外,您可以使用 Session.Load() 避免数据库命中:

temp.Company = _session.Load<Company>(passedInCompanyId);

I'm not 100% sure, but it really looks like maybe there is a bug here:

temp.Company = ...(temp.Company.Id);

I would figure you'd actually be pulling that from an incoming parameter.

Also, you can avoid a database hit here by using Session.Load():

temp.Company = _session.Load<Company>(passedInCompanyId);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文