gpt4 book ai didi

fluent-nhibernate - FluentNHibernate : Getting Column & Table Names After Mapping Conventions Are Applied

转载 作者:行者123 更新时间:2023-12-03 23:40:12 33 4
gpt4 key购买 nike

我想知道是否可以找到 运行时 已用 映射的类/组件的列名FluentNHibernate 在应用了所有约定之后。

例如,给定简单模型:

 public class Address{ 
public string Street {get; set;}
public string Zip {get; set;}
}

public class AddressMap : ComponentMap<Address>{
Map( x => x.Street );
Map( x => x.Zip );
}

public class PersonMap : ClassMap<Person>
{
public PersonMap(){

Id( x => x.Id );
Map( x=> x.Ssn );
Map( x=> x.Name );

Component( x => x.Address )
.ColumnPrefix("ADDRESS_");
}
}

public class ClassConvention : IClassConvention
{
public void Apply( IClassInstance instance )
{
instance.Table( "tbl" + instance.EntityType.Name );
}
}

表名: tblPerson
  Id     Name     Ssn         ADDRESS_Street     ADDRESS_Zip
-----------------------------------------------------------
1 Brian 11223344 123 Example St. 12345

我正在寻找但我不知道该怎么做的是以下内容:
var mappings = FluentNHibaernate.CompileMergeAndBuildAllMappings();

var zipCodeColumnName = mappings.FindMappedType<Address>().ColumnName(a => a.Zip)

zipCodeColumnName.ShouldBe("ADDRESS_Zip");
// Here I'm interested in the run-time & final column name with the
// prefix applied from the PersonMap class.

var personTableName = mappings.FindMappedType<Person>().TableName;
personTableName.ShouldBe("tblPerson");
// I'm interested in the final table name after the ClassConvention
// modified the table name.

附加说明
  • 我只对 的结果感兴趣FluentNHiberante 的约定和映射的应用。不是在由 NHibernate 生成的实际 SQL 中。

  • 谢谢您的帮助,
    布赖恩

    最佳答案

        [Test]
    public void test4()
    {
    var ssnColumn = RuntimeNames
    .ColumnName<Person>( x => x.Ssn );
    ssnColumn.ShouldEqual( "Ssn" );

    var addressColumn = RuntimeNames
    .ColumnName<Person>( x => x.Address.Street );
    addressColumn.ShouldEqual( "ADDRESS_Street" );

    var personTableName = RuntimeNames
    .TableName<Person>();
    personTableName.ShouldEqual( "tblPerson" );
    }
    public static class RuntimeNames
    {
    private static Configuration cfg = Fluently.Configure()
    .Database( MsSqlConfiguration.MsSql2005 )
    .Mappings( m => m.FluentMappings
    .AddFromAssemblyOf<PersonMap>()
    .Conventions
    .AddFromAssemblyOf<PersonMap>()
    ).BuildConfiguration();

    public static string ColumnName<T>( Expression<Func<T, object>> property )
    where T : class, new()
    {
    var accessor = FluentNHibernate.Utils.Reflection
    .ReflectionHelper.GetAccessor( property );

    var names = accessor.Name.Split('.');

    var classMapping = cfg.GetClassMapping( typeof( T ) );

    return WalkPropertyChain( classMapping.GetProperty(names.First()), 0, names );
    }

    private static string WalkPropertyChain(Property property, int index, string[] names)
    {
    if( property.IsComposite )
    return WalkPropertyChain( ((Component)property.Value).GetProperty( names[++index] ), index, names );

    return property.ColumnIterator.First().Text;
    }

    public static string TableName<T>() where T : class, new()
    {
    return cfg.GetClassMapping( typeof(T) )
    .Table.Name;
    }
    }

    关于fluent-nhibernate - FluentNHibernate : Getting Column & Table Names After Mapping Conventions Are Applied,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9460522/

    33 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com