user781916
user781916

Reputation:

Fluent Nhibernate: Cant delete an item with as HasMany property

I dont normally deal with data like this but I thought id give it a try. As it turned out I failed :/ and am not sure how to proceed.

I have a database object track:

public virtual string Type { get; set; }

public virtual IList<DateTypeTrack> TrackDates { get; set; }

With a mapping file:

Table("Tracks");
Map(x => x.Type).Not.Nullable();
HasMany(x => x.TrackDates).KeyColumn("TrackID").Cascade.All();

The DateTypeTrack Object looks like this:

public virtual DateType DateType { get; set; }
public virtual Track Track { get; set; }
public virtual int Days { get; set; }

With a mapping file like this:

Table("DateTypeTracks");
References(x => x.DateType, "DateTypeID").Not.Nullable();
References(x => x.Track, "TrackID").Not.Nullable();
Map(x => x.Days).Not.Nullable();

If its necessary, Ill post the DateType code aswell, but I dont think its needed.

And am trying to write a delete method in my service layer that is pretty simple:

public void PushDelete(int id)
    {
        Track track = _tracks.Get(id);

        try
        {
            _tracks.BeginTransaction();
            _tracks.Delete(track);
            _tracks.CommitTransaction();
        }
        catch (Exception)
        {
            _tracks.RollbackTransaction();
            throw;
        }
    }

I keep getting an error:

could not delete collection: [TSE.Domain.DatabaseObjects.Track.TrackDates#12][SQL: UPDATE DateTypeTracks SET TrackID = null WHERE TrackID = @p0]

I dont know why its trying to do the update at the end, but I suppose that is what is causing the issue. What sort of recourse do I have?

Thanks.

Upvotes: 1

Views: 1541

Answers (1)

Firo
Firo

Reputation: 30813

since the DateTypeTrack already cares for the association between the two entities you should mark the HasMany as Inverse to tell NH that the hasmany does not maintain it (the Update)

HasMany(x => x.TrackDates).KeyColumn("TrackID").Cascade.All().Inverse();

Upvotes: 2

Related Questions