jeudi 22 octobre 2015

FluentNHibernate.Testing test Map with FK constrain

I am trying to create Map integration test using FluentNHibernate.Testing and C#.

I have 2 tables with 1 to many relationship. OrderSet entity contain list of Order entities referenced by Orders.SetId column which is FK.

Table OrderSet {
  SetId [nvarchar] not null
}

Table Orders {
  OrderId [bigint] not null,
  SetId [nvarchar]// This is FK referencing Set table
}

Also I have following NHibernate map for Orders table. OrderSet table is not mapped by Order map (OrderSet map do contain HasMany(...) mapping):

internal sealed class OrderMap : ClassMap<Order>
{
  public OrderMap()
  {
    Proxy<IOrder>();
    Schema("dbo");
    Table("Orders");
    Id(m => m.Id).Column("OrderId").GeneratedBy.Assigned();
  }
}

Here is code sniped from test file:

new PersistenceSpecification<Order>(session)
  .CheckProperty(t => t.Id, 123)
  .VerifyTheMappings();

During test execution I am receiving error: can not insert NULL as SetId into OrderSet table.

Test tries to insert new record into Order table but because there is FK constrain and SetId is set to NULL (default value) such statement can not be executed: nulls are not allowed in OrderSet.SetId column.

I was not able to find a proper way to test NHibernate Map of entity with FK. All examples are dealing with root entity and do not compare mapping of children.

Is there is a way to create an integration test for entity map which have FK column while that column is not mapped in that entity (since it is not required by domain model)?

Aucun commentaire:

Enregistrer un commentaire