流畅的NHibernate映射与聚合类

时间:2016-01-29 23:19:44

标签: c# sql-server nhibernate fluent-nhibernate fluent-nhibernate-mapping

我需要有关聚合的类的流畅NHibernate映射配置的帮助。

以下代码显示具有类型为Image的属性Logo的类Provider。我的问题是属性Logo.EntityId应该有Provider.Id

当我使用我的存储库在SQLite数据库中保存类Provider时(也使用MSSQL测试),抛出一个NHibernate异常:

  

FOREIGN KEY约束失败。

持久模型定义:

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);
    }
}

映射:

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();
    }
}

0 个答案:

没有答案