开发者

AutoMapping with FluentMapping doesn't quite seem to work for me

I'm a n00b. Here's what I want to do:

Use AutoMapping to configure every property between the model -> table. Then I would like to override 2 specific items in a fluent map. The two items are: Id & Table name.

So my Maps look like this:

public class BillMasterMap : ClassMap<BillMaster>
{
    public BillMasterMap()
    {
        Table("BILLMAST");
        Id(x => x.SYSKEY);
    }
}

And my factory settings look like this:

public static ISessionFactory SessionFactory(string connectionString)
    {
        if (_sessionFactory == null)
        {
            _sessionFactory = Fluently.Configure()
                .Database(MsSqlConfiguration.MsSql2005.ShowSql()
                        .ConnectionString(c => c.Is(connectionString)).Cache(c => c.UseQueryCache().ProviderClass<HashtableCacheProvider>()))
                .Mappings(m => 
                    m.AutoMappings.Add(AutoMap.AssemblyOf<BillMaster>()
                                .Where(x => x.Namespace.EndsWith("Entities"))))
                .Mappings(m => 
                    m.FluentMappings.AddFromAssemblyOf<BillInvoiceMap>())
                .BuildSessionFactory();
        }

        return _sessionFactory;
    }

The problem is that FNH finds the Id override for BillMaster, but not for BillInvoice which looks like开发者_开发技巧 this (identical it seems)

public class BillInvoiceMap : ClassMap<BillInvoice>
{
    public BillInvoiceMap()
    {
        Id(x => x.SYSKEY);
        Table("BILLINV");
    }
}

I've gotten around the problem by configuring my automapping with the Setup() as shown below:

 .Mappings(m => 
                    m.AutoMappings.Add(AutoMap.AssemblyOf<BillMaster>()
                                .Setup(s => s.FindIdentity = property => property.Name == "SYSKEY")            
                                .Where(x => x.Namespace.EndsWith("Entities"))))

But I would like to combine auto & fluent as other tables don't use "SYSKEY" as their Id column.

Thoughts? Is this a FNH bug?


Got it working.

1.) I have to setup AutoMapping with the Setup method I described above 2.) Additionally I have to setup fluentmappings with the Id method

when both are setup like this, then it works for me.

So

public static ISessionFactory SessionFactory(string connectionString)
    {
        if (_sessionFactory == null)
        {
            _sessionFactory = Fluently.Configure()
                .Database(MsSqlConfiguration.MsSql2005.ShowSql()
                              .ConnectionString(c => c.Is(connectionString)).Cache(
                              c => c.UseQueryCache().ProviderClass<HashtableCacheProvider>()))
                .Mappings(m =>
                              {
                                  m.FluentMappings.AddFromAssemblyOf<BaseEntity>();
                                  m.AutoMappings.Add(AutoMap.AssemblyOf<BaseEntity>()
                                                         .Setup(s => s.FindIdentity = property => property.Name == "SYSKEY")
                                                         .Where(x => x.Namespace.EndsWith("Entities")));

                              })
                .BuildSessionFactory();
        }

        return _sessionFactory;
    }

And the map

 public class BillInvoiceMap : ClassMap<BillInvoice>
{
    public BillInvoiceMap()
    {
        Table("BILLINV");
        Id(x => x.SYSKEY);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜