0

我试过到处搜索这方面的信息,但似乎没有人能完全涵盖这个问题。我已将其简化为以下示例。

这是我的数据库:

create table ThingTypes
(
    Id int,
    Name varchar(50) not null

    constraint PK_ThingTypes primary key (Id)
)
go

insert into ThingTypes(Id, Name) Values(1, 'ThingTypeOne')
insert into ThingTypes(Id, Name) Values(2, 'ThingTypeTwo')

create table Things
(
    Id int identity,
    Name varchar(50) not null,
    ThingTypeId int not null

    constraint PK_Things primary key (Id),
    constraint FK_Things_ThingTypes foreign key (ThingTypeId) references ThingTypes(Id)
)
go

这是我的模型:

public abstract class Thing
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
}
public class ThingTypeOne : Thing { }
public class ThingTypeTwo : Thing { }

这些是我的映射:

public class ThingOverride : IAutoMappingOverride<Thing>
{
    public void Override(AutoMapping<Thing> mapping)
    {
        mapping.DiscriminateSubClassesOnColumn("ThingTypeId", 0);
    }
}

public class ThingTypeOneMap : SubclassMap<ThingTypeOne>
{
    public ThingTypeOneMap()
    {
        DiscriminatorValue(1);
    }
}

public class ThingTypeTwoMap : SubclassMap<ThingTypeTwo>
{
    public ThingTypeTwoMap()
    {
        DiscriminatorValue(2);
    }
}

这是我的 ISessionFactory 构建器:

public static ISessionFactory GetFactory(string connectionString)
{
    var configuration = new Configuration().AddProperties(new Dictionary<string, string>
        {
            {Environment.ConnectionDriver, typeof (SqlClientDriver).FullName},
            {Environment.Dialect, typeof (MsSql2008Dialect).FullName},
            {Environment.ConnectionProvider, typeof (DriverConnectionProvider).FullName},
            {Environment.ConnectionString, connectionString},
            {Environment.ShowSql, "true"},
            {Environment.BatchSize, "100"},
            {Environment.Isolation, "ReadCommitted"}
        });

    return Fluently.Configure(configuration)
                    .Mappings(m =>
                        {
                            m.FluentMappings.AddFromAssemblyOf<ThingTypeOneMap>();
                            m.AutoMappings.Add(AutoMap.AssemblyOf<Thing>(new DefaultAutomappingConfiguration())
                                                        .UseOverridesFromAssemblyOf<ThingOverride>()
                                );
                        })
                    .BuildSessionFactory();
}

这是我失败的测试:

var sessionFactory = SessionFactoryBuilder.GetFactory(@"Data Source=localhost;Initial Catalog=ThingDatabase;Trusted_Connection=True;");
using (var session = sessionFactory.OpenSession())
{
    session.BeginTransaction();
    session.SaveOrUpdate(new ThingTypeOne
        {
            Name = "New Thing Type One"
        });
    session.Transaction.Commit();
}

它失败了,因为它试图插入显然不存在的表“ThingTypeOne”。我需要做什么才能让 Fluent NHibernate 真正关注我设置的 DiscriminateSubClassesOnColumn 属性?

4

1 回答 1

2

而不是每个子类的子类映射添加一个约定

class DiscriminatorValueConvention : ISubclassConvention
{
    public void Apply(ISubclassInstance instance)
    {
        if (instance.Type == typeof(ThingTypeOne))
        {
            instance.DiscriminatorValue(1);
        }
        else if (instance.Type == typeof(ThingTypeTwo))
        {
            instance.DiscriminatorValue(2);
        }
    }
}
于 2013-11-04T09:35:44.667 回答