Fluent nhibernate Fluent NHibernte变更/约定

Fluent nhibernate Fluent NHibernte变更/约定,fluent-nhibernate,conventions,Fluent Nhibernate,Conventions,我喜欢我在这篇博客文章()中看到的模式,作者在应用约定之前检查表名是否已经更改 public bool Accept(IClassMap target) { //apply this convention if table wasn't specified with WithTable(..) method return string.IsNullOrEmpty(target.TableName); } 我用于字符串长度的约定接口是IProperty: public class

我喜欢我在这篇博客文章()中看到的模式,作者在应用约定之前检查表名是否已经更改

public bool Accept(IClassMap target)
{
    //apply this convention if table wasn't specified with WithTable(..) method
    return string.IsNullOrEmpty(target.TableName);
}
我用于字符串长度的约定接口是IProperty:

public class DefaultStringLengthConvention: IPropertyConvention
{
    public bool Accept(IProperty property) {
        //apply if the string length hasn't been already been specified
        return ??; <------ ??
    }

    public void Apply(IProperty property) {
        property.WithLengthOf(50);
    }
}
公共类DefaultStringLength约定:IPropertyConvention
{
公共bool Accept(IProperty属性){
//如果尚未指定字符串长度,则应用

return???;
。WithLengthOf()
在生成XML映射时向要应用的更改列表中添加一个“更改”(
操作
)。不幸的是,该字段是
私有的
,并且没有访问更改列表的属性,因此(目前)恐怕有无法检查属性映射是否应用了长度为

除非出现更好的替代方案,否则您可以使用
HasAttribute(“长度”)

在代码中阐明Stuart和Jamie在说什么,以下是有效的方法:

public class UserMap : IAutoMappingOverride<User>
{
    public void Override(AutoMap<User> mapping) {
        ...
        const int emailStringLength = 30;
        mapping.Map(x => x.Email)
            .WithLengthOf(emailStringLength)                        // actually set it
            .SetAttribute("length", emailStringLength.ToString());  // flag it is set
        ...

    }
}

public class DefaultStringLengthConvention: IPropertyConvention
{
    public bool Accept(IProperty property) {
        return true;
    }

    public void Apply(IProperty property) {
        // only for strings
        if (!property.PropertyType.Equals(typeof(string))) return;

        // only if not already set
        if (property.HasAttribute("length")) return;

        property.WithLengthOf(50);
    }
}
public类UserMap:IAutoMappingOverride
{
公共无效替代(自动映射映射){
...
const int emailStringLength=30;
Map.Map(x=>x.Email)
.WithLengthOf(emailStringLength)//实际设置它
.SetAttribute(“长度”,emailStringLength.ToString());//标记已设置
...
}
}
公共类DefaultStringLength约定:IPropertyConvention
{
公共bool Accept(IProperty属性){
返回true;
}
公共无效应用(IProperty属性){
//仅适用于字符串
如果(!property.PropertyType.Equals(typeof(string)))返回;
//除非尚未设置
if(property.HasAttribute(“length”))返回;
财产。长度为(50);
}
}

我认为这可能就是问题所在;我不介意为字符串长度组添加约定,但许多情况是由类驱动的一次性情况。因此,如果EmployeeNumber字符串长度应为6个字符,则一个字段的EmployeeNumberStringLength约定或DefaultStringLength.Thx中的if语句为了快速响应。那就足够了,但它不起作用。我已经几周没有更新FNH了-这是最近的更新吗?属性可能不是“长度”吗?谢谢你的回答。Berryl,如果你使用SetAttribute,HasAttribute应该起作用。从版本458开始,WithLengthOf存储“长度”集合中名为columnProperties的属性。SetAttribute存储在extendedProperties中,这也是HasAttribute的外观。据我所知,SetAttribute/HasAttribute以这种方式工作了很长时间,因此它可能适用于您正在使用的任何版本。