2

我有一种有趣的感觉,这只是我对 nHibernate 和 Fluent nHibernate 的理解。

我有一个小型控制台应用程序,它基于一堆实体类导出模式。在我这样做之前,一切都很好:

    public virtual int UserId { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
    public virtual string Email { get; set; }
    public virtual UserType UserType { get; set; }
    public virtual Client Client { get; set; }
    public virtual string UniqueIdentifier { get; set; }

    public virtual IList<UserAttribute> UserAttributes { get; set; }

    /** CODE IN QUESTION **/
    public virtual string GetAttributeValue(string name)
    {
        var value = UserAttributes.FirstOrDefault(ua => ua.AttributeDefinition.AttributeName == name);
        if (value != null)
            return value.Value;
        else
            return string.Empty;
    }

如果我注释掉这个函数,一切都会很好。即使我只有函数返回 string.Empty,一切正常。但是,一旦我添加回该行:

var value = UserAttributes.FirstOrDefault(ua => ua.AttributeDefinition.AttributeName == name);

一切都因此错误而中断:

"The entity '<>c__DisplayClass1' doesn't have an Id mapped. Use the Id method to map your identity property. For example: Id(x => x.Id)."

我正在使用自动映射。这个错误的原因是什么?我通常理解错误,但看起来它在匿名类上失败了。我只是对这一切应该如何协同工作有一个严重的误解吗?我绝对是一个 nHibernate 新手,无论是在实践上还是在哲学上。我很想开悟!

4

2 回答 2

6

lambdaua => ua.AttributeDefinition.AttributeName == name生成一个内部类来捕获 name 参数。自动映射器遍历每种类型并尝试对其进行映射,但不幸的是,它还选择了编译器生成的类型。

您可以实现 AutomappingConfiguration 以排除编译器生成的类

class MyAutomappingConfiguration : DefaultAutomappingConfiguration
{
    public override bool ShouldMap(Type type)
    {
        return !type.IsDefined(typeof(CompilerGeneratedAttribute), false) && base.ShouldMap(type);
    }
}
于 2012-07-12T08:37:01.043 回答
0

虽然,上面的@Firo 答案有效,但当您在另一个 Linq 查询中有 Linq 查询时,它似乎不起作用。例子:

DataTable Dt = FillData();
EnumerableRowCollection<DataRow> MasterProfileRows = null;
IList<RecordOwner> OwnedRecs = new List<RecordOwner>();
OwnedRecs.Add(new RecordOwner { ForeignRecordKey = 100 });
MasterProfileRows = from x in dt.AsEnumerable()
                    where OwnedRecs.Any(y => y.ForeignRecordKey.ToString() == x.Field<object>("ProfileID").ToString())
                    select x;

为了解决这个问题,我将它添加到我的覆盖中,以检查类的 FullName 中“__displayclass”的出现次数,如果它大于 1,我们将忽略它的映射。

public override bool ShouldMap(Type type)
{
    // there is a bug where the fluent automapper tries to automap anonymous types within a lambda. the !type.IsDefined(typeof(CompilerGeneratedAttribute), false) works around this issue
    // except where there is a nest linq query.
    // see: 
    // https://github.com/jagregory/fluent-nhibernate/issues/146
    // http://stackoverflow.com/questions/11446155/fluent-nhibernate-not-generating-schema-correctly-on-function/11447966#11447966
    // http://stackoverflow.com/questions/14268568/odd-error-building-fluentnh-configuration
    // http://stackoverflow.com/questions/11446155/fluent-nhibernate-not-generating-schema-correctly-on-function/11447966
    // ignore nested linq queries (linq queries within linq queries) because they cause the auto mapper to error out.
    System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex("__displayclass", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
    if(reg.Matches(type.FullName).Count > 1)
        return false;

    return !type.IsDefined(typeof(CompilerGeneratedAttribute), false);
}
于 2013-10-22T16:59:24.953 回答