2

I need help with a fluent NHibernate map configuration of a class with aggregation.

The following code displays the class Provider with property Logo of type Image. My problem is that the property Logo.EntityId should have the Provider.Id.

When I save the class Provider with my repository in the SQLite database (tested also with MSSQL), throws an NHibernate exception:

FOREIGN KEY constraint failed.

Persistance model defintions:

public class Provider
{
    private IList<AffiliateData> _affiliateDatas;
    private IList<Localization> _shipping;

    public Guid Id { get; set; }

    public string WebsiteUrl { get; set; }

    public Country Country { get; set; }

    public string DefaultCurrency { get; set; }

    public DateTime? ActivationDate { get; set; }

    public bool IsActive { get { return ActivationDate.HasValue; } }

    public decimal Commission { get; set; }

    public ContractState Contract { get; set; }

    public bool AreNetPrices { get; set; }

    public decimal Tax { get; set; }

    public TimeSpan PriceUpdateInterval { get; set; }

    public AffiliateData[] AffiliateDatas
    {
        get { return _affiliateDatas.ToArray(); }
        set { _affiliateDatas = value; }
    }

    public Image Logo { get; set; }

    public Localization[] Shipping
    {
        get { return _shipping.ToArray(); }
        set { _shipping = value; }
    }

    public PaymentType PaymentType { get; set; }

    public DateTime LastPriceUpdate { get; set; }

    public DataAccessType AccessType { get; set; }

    public Provider()
    {
        _affiliateDatas = new AffiliateData[0];
        _shipping = new Localization[0];
    }
}

public class Image
{
    public Guid Id { get; set; }

    public Guid EntityId { get; set; } // <----- have to equals with Provider.Id

    public File Large { get; set; }

    public File Small { get; set; }

    public File Thumbnail { get; set; }

    public override string ToString()
    {
        return string.Format("{0} {1}", EntityId, Large);
    }
}

Mappings:

public class ProviderMap : ClassMap<Provider>
{
    public ProviderMap()
    {
        Not.LazyLoad();
        Table(TableNames.Provider);
        Id(p => p.Id).Not.Nullable().GeneratedBy.Assigned().Index("PK_Provider_Id");
        Map(p => p.Type);
        Map(p => p.ModificationDate);
        Map(p => p.CreateDate)
            .Not.Nullable()
            .Default("CURRENT_TIMESTAMP")
            .ReadOnly()
            .Generated.Insert();
        Map(p => p.ActivationDate);
        Map(p => p.AreNetPrices);
        Map(p => p.Commission);
        Map(p => p.Contract);
        Map(p => p.DefaultCurrency);
        Map(p => p.PaymentType);
        Map(p => p.PriceUpdateInterval);
        Map(p => p.Tax);
        Map(p => p.WebsiteUrl);
        Map(p => p.LastPriceUpdate).Nullable();
        Map(p => p.AccessType)
            .CustomType<int>()
            .Not.Nullable()
            .Default("0");
        HasMany(p => p.AffiliateDatas)
            .Not.LazyLoad()
            .Access.CamelCaseField(Prefix.Underscore)
            .KeyColumns.Add("ProviderId", c => c.Index("IX_AffiliateData_ProviderId"))
            .Cascade.All();
        HasMany(p => p.Shipping)
            .Not.LazyLoad().Access
            .CamelCaseField(Prefix.Underscore)
            .Cascade.All()
            .Where(LocalizationTypeClause.Shipping);
        References(p => p.Country)
            .Column("CountryId")
            .Not.LazyLoad()
            .Cascade.Persist();
        References(p => p.Logo)
            .Column("LogoId")
            .Not.LazyLoad()
            .Cascade.All();
    }
}

public class ImageMap : ClassMap<Image>
{
    public ImageMap()
    {
        Not.LazyLoad();
        Table(TableNames.Image);
        Id(p => p.Id).Not.Nullable().GeneratedBy.Assigned().Index("PK_Image_Id");
        Map(p => p.EntityId); // <------------------- FOREIGN KEY constraint failed
        Map(p => p.Type);
        Map(p => p.ModificationDate);
        Map(p => p.CreateDate)
            .Not.Nullable()
            .Default(SqliteDataTypes.DefaultDateTime)
            .ReadOnly()
            .Generated.Insert();
        References(p => p.Large)
            .Column("LargeImageId").Index("IX_Image_LargeImageId")
            .Not.LazyLoad()
            .Cascade.All();
        References(p => p.Small)
            .Column("SmallImageId").Index("IX_Image_SmallImageId")
            .Not.LazyLoad()
            .Cascade.All();
        References(p => p.Thumbnail)
            .Column("ThumbnailId").Index("IX_Image_ThumbnailId")
            .Not.LazyLoad()
            .Cascade.All();
    }
}
kayess
  • 3,384
  • 9
  • 28
  • 45
user2209131
  • 275
  • 2
  • 11

0 Answers0