NHibernate One-To-Many Problem

I have a Vessel object that has a one-to-many relationship with the VesselDetail Object. When I add the VesselDetail object to the Vessel object and try to save the Vessel object, it seems that NHibernate does not add the foreign key when inserting the VesselDetail object.

Where am I wrong here? I just can't understand.

Error message: BDN.FindVessel.Tests.Integration.NhibernateRepositoryTests.SaveVessel_ShouldAddDetailsToDb_WhenAddedToEntity: NHibernate.Exceptions.GenericADOException: Cannot insert: [BDN.FindVessel.Domain.VesselDetSailGear Other , TranslatorId, SpeenAndConsumption, MainMachinery, created, class, features, culture, Interior, Electronics, DeckGear) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,? ,?); select SCOPE_IDENTITY ()] ----> System.Data.SqlClient.SqlException: cannot insert a NULL value into the "BoatId" column, the table "FindVesselTest.dbo.BoatsDetails"; column does not allow zeros. INSERT fails. Application completed.

public class Vessel
{
        public virtual int BoatId { get; set; }
        public virtual IList<VesselDetail> Details { get; set; }
        //...
}

public class VesselDetail
{
        public virtual int VesselDetailId { get; set; }
        //some other properties
        //..
}

public class VesselMap: ClassMap<Vessel>
{
    public VesselMap()
    {
        WithTable("Boats");

        Id(x => x.BoatId, "Id");

        //..

        HasMany(x => x.Details)
            .WithKeyColumn("BoatId") //foreign key in the BoatsDetails table
            .Cascade.All();
    }
}

public class VesselDetailMap:ClassMap<VesselDetail>
{
  public VesselDetailMap()
  {
      WithTable("BoatsDetails");

      Id(x => x.VesselDetailId, "Id");

      //...
  }
}
+3
1

, NHibernate. , , , " ".

:

public class VesselMap: ClassMap<Vessel>
{
    public VesselMap()
    {
        //...

        HasMany(x => x.Details)
                .Inverse()
                .WithKeyColumn("BoatId");
    }
}

public class VesselDetailMap:ClassMap<VesselDetail>
{
  public VesselDetailMap()
  {
      //..

       References(x => x.Vessel, "BoatId")
                .Cascade
                .SaveUpdate();
  }
}
+10

Source: https://habr.com/ru/post/1703330/


All Articles