0

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.

1
  • 1
    You have some lacking and confusing info here. In the last code section, dont you want to do _session.Update(temp); ? If not, what is c? Did you get temp/c from the same session that you pass to the company repo? Also, you see that you are basically setting it back to its self when you call the repo line correct?
    – CrazyDart
    Oct 21, 2010 at 18:17

2 Answers 2

2

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.

4
  • That is what im doing. let me see if that fixes it. Oct 21, 2010 at 18:31
  • You got it! I put a new parameter in the controller action, renamed the dropdownlist that controls it so that it passes in the company id to the controller action and used session.load<Company>(companyId) Oct 21, 2010 at 18:59
  • Sucks that i cant set both of you as the right answer since you both helped me. Oct 21, 2010 at 18:59
  • yah, we need partial answer abilities... glad you got it fixed. Just remember, you see the POCO as just a POCO, but the reason you have to mark everything with virtual is because NHibernate will go in and add is secret sauce so it can track ALL changes. This is why those things are hard to serialize too... they have extra junk on them.
    – CrazyDart
    Oct 21, 2010 at 19:28
1

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);
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.