Dimitar Dimitrov
Dimitar Dimitrov

Reputation: 15148

Fluent NHibernate Relation Without Foreign Key

I'm trying so simplify my problem here, but basically I'm trying to map 2 entities however i don't have a Foreign Key in the database set, since the column could be null. When I try to do an insert on the parent, I'm getting the following error:

object references an unsaved transient instance - save the transient instance before flushing or set cascade action for the property to something that would make it autosave.

This is what I have so far:

My entities

public class DocumentDraft
{
    public virtual Guid Id { get; set; }
    public virtual string Subject { get; set; }
    public virtual string ReferenceNo { get; set;}
    public virtual DocumentType DocumentType { get; set; }
}

public class DocumentType
{
    public virtual short Id { get; set; }
    public virtual string Description { get; set; }
}

Mapping

public class DocumentDraftMap : ClassMap<DocumentDraft>
{
    public DocumentDraft()
    {
        // other mappings ...
        References(x => x.DocumentType)
            .Columns("DocumentTypeId")
            .Nullable()
            .Not.LazyLoad()
            .NotFound.Ignore(); // <-- added this since the value could be null and it throws an error
    }
}

I tried specifying Cascade.None() in the mapping, but I'm getting the same result. Basically what happens is that a null value is attempted at being inserted in the DocumentType, and I don't want this (I want to insert null in the parent table, but I don't want to touch the child tables at all, I don't want this to cascade).

I've also tried: .Not.Insert(), but that didn't work either.

I'd appreciate it if someone could help me out on this one.

Upvotes: 0

Views: 749

Answers (1)

Firo
Firo

Reputation: 30813

I guess the property DocumentType is not really null when saving. It seems there is an instance and without Cascade.All() on the reference it can not be saved.

Upvotes: 2

Related Questions