列属性AutoMapping

时间:2009-08-21 10:11:18

标签: fluent-nhibernate nhibernate-mapping automapping

可以为列命名创建约定:

我有这段代码:

public AutoPersistenceModel Generate()
{
    var result = AutoPersistenceModel.MapEntitiesFromAssemblyOf<User>()
        .Where(GetAutoMappingFilter)
        .WithConvention(GetConventions);

    return result;
}

private bool GetAutoMappingFilter(Type t)
{
    return
        t.GetInterfaces().Any(
            x => x.IsGenericType && (x.GetGenericTypeDefinition() == typeof(IEntityWithTypedId<>)));
}


private static void GetConventions(Conventions conventions)
{
    conventions.GetPrimaryKeyNameFromType = type => type.Name.ToLower() + "_id";
    conventions.FindIdentity = type => type.Name.ToLower() == "id";
    conventions.GetTableName = type =>
        {
            if (!type.Name.Contains("Lookup"))
            {
                return Inflector.Net.Inflector.Pluralize(type.Name).ToLower();
            }
            return Inflector.Net.Inflector.Underscore(type.Name)
                .Replace("lookup", "lu").ToLower();
        };
    conventions.IsBaseType = DontMapAsJoinedSubclassTypesInheritedFrom;
    conventions.GetForeignKeyNameOfParent = type => Inflector.Net.Inflector.Underscore(type.Name) + "_id";
    conventions.GetForeignKeyName = type => Inflector.Net.Inflector.Underscore(type.Name) + "_id";
    conventions.OneToManyConvention = m => m.Cascade.All();

}


private static bool DontMapAsJoinedSubclassTypesInheritedFrom(Type arg)
{
    var derivesFromEntity = arg == typeof(Entity);
    var derivesFromEntityWithTypedId = arg.IsGenericType &&
        (arg.GetGenericTypeDefinition() == typeof(EntityWithTypedId<>));
    return derivesFromEntity || derivesFromEntityWithTypedId;
}

和实体类

public class Organisation : EntityWithTypedId<int>
{
    public virtual Organisation Parent { get; set; }
    public virtual LookOrganisationType OrganisationType { get; set; }
    [DomainSignature]
    public virtual string OrganisationName { get; set; }
    public virtual string Address1 { get; set; }
    public virtual string Address2 { get; set; }
    public virtual string City { get; set; }
    public virtual string Postcode { get; set; }
    public virtual LookupCountry Country { get; set; }
    public virtual string TelNo { get; set; }
    public virtual string FaxNo { get; set; }
    public virtual string Email { get; set; }
    public virtual User Contact { get; set; }
    public virtual DateTime? DateCreated { get; set; }
    public virtual DateTime? DateAmended { get; set; }
    public virtual bool Active { get; set; }
}

最后我想要一个列,让我们说TelNo就像tel_no一样。 因此,如果财产中包含大写字母,则该公约应予以强调。 Inflector.Net.Inflector.Underscore工作正常。但我不知道怎么写会议。

Somethig喜欢:

(conventions.GetPropertyName = type => Inflector.Net.Inflector.Underscore(type.ColumnName))

由于

0 个答案:

没有答案