0

I'm trying to add a new object with nHibernate, but for some reason it tries to update a field on a referenced object that hasn't been provided resulting in:

Cannot insert the value NULL into column 'Value', table 'MyDB.dbo.Type'; 
column does not allow nulls. UPDATE fails.

Lets assume there's a [Type] table with the record { Id = 1, Value = "Type1" }

I want to add an Item record with code like:

var item = new Item()
{
    Name = "New Item",
    Type = new Type() { Id = 1 }
}
await _session.SaveAsync(item);

The save status fails presumably because it tries to overwrite the type record { Id = 1, Value = "Type1" } by { Id = 1, Value = null }

The objects are styructured as followed:

public class Type
{
    public int Id { get; set }
    public string Value { get; set }
}

public class Item
{
    public int Id { get; set }
    public string Name { get; set }
    public Type Type { get; set }
}

And the mapping of them is:

public class TypeMapping : ClassMap<Type>
{
    public TypeMapping()
    {
        Table("[Type]");
        Id(x => x.Id).GeneratedBy.Identity();
        Map(x => x.Value);

        OptimisticLock.Version();
    }
}

public class ItemMapping : ClassMap<Item>
{
    public ItemMapping()
    {
        Table("[Item]");
        Id(x => x.Id).GeneratedBy.Identity();
        Map(x => x.Name);
        References(x => x.Type, "TypeId")
            .Cascade.None()
            .Fetch.Select();
        OptimisticLock.Version();
    }
}

What do I need to do to make sure it just links the type to the new item instead of trying to overwrite the Type record?

1
  • You need to pull the existing Type form the db and use that instead of newing up one... Commented Mar 11, 2022 at 18:28

2 Answers 2

1

A colleague of mine became available and he gave me a quick solution to my problem.

var item = new Item()
{
    Name = "New Item",
    Type = Session.Load<Type>(1)
}
await _session.SaveAsync(item);

Apparently that Load command doesn't actually load anything as long as you don't use any properties on the "loaded" object.

That way it's linking to the referenced record, just as I wanted.

Sign up to request clarification or add additional context in comments.

Comments

0

when you're trying to make a Type you only put in one parameter for:

Id {get, set}
Value {get, set}

so it'll fill the string one with NULL if you want it to have the string as "Type1" you should do instead:

    Type = new Type() { Id = 1 , Value = "Type1"}

2 Comments

I'm not trying to add a Type, I'm adding an Item that references an already existing Type. I do not want to change the Value field of the existing Type, just link it to the new Item
@FoxHound i don't think you can do it like that. Are you maybe thinking of the way a Mapper<Key, Value> works?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.