【问题标题】:fluent nhibernate not generating schema correctly on function流利的休眠没有在函数上正确生成模式
【发布时间】:2012-07-12 06:30:18
【问题描述】:

我有一种有趣的感觉,这只是我对 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 新手,无论是在实践上还是在哲学上。我很想开悟!

【问题讨论】:

    标签: nhibernate fluent-nhibernate


    【解决方案1】:

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

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

    class MyAutomappingConfiguration : DefaultAutomappingConfiguration
    {
        public override bool ShouldMap(Type type)
        {
            return !type.IsDefined(typeof(CompilerGeneratedAttribute), false) && base.ShouldMap(type);
        }
    }
    

    【讨论】:

    • Firo - 我的下意识反应是这种行为是 Automapping 中的错误 - 你同意吗?
    • 我自己也在想同样的事情......这看起来不错,我会在接下来的一个小时左右测试它。
    • @TomBushell 我会这么说。如果是,请提出问题。
    • 真棒@Firo 这为我解决了它。我在 FNH 1.3.0.733 和 NH v3.3.1.4000 上,这些版本中仍然存在错误。我没有在 github 上的问题列表中看到它,所以不确定他们是否知道。
    • 更正-这是github帖子Automap tries to map anonymous classes
    【解决方案2】:

    虽然,@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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多