【问题标题】:Map an Id from an Identity class with Fluent NHibernate使用 Fluent NHibernate 从 Identity 类映射一个 Id
【发布时间】:2012-08-16 08:42:12
【问题描述】:

我正在尝试使用 Fluent NHibernate 映射来自 Identity 类的 Id。

身份类别:

public interface IValueObject<T> {
    bool SameValueAs(T other);
}

[Serializable]
public class Identity<TEntity> : IValueObject<Identity<TEntity>> {

    public long Id { get; protected set; }

    public Identity(long id) {
        this.Id = id;
    }

    protected Identity() { }

    public bool SameValueAs(Identity<TEntity> other) {
        return other != null && this.Id == other.Id;
    }
}

型号:

public interface IEntity<T> {
    Identity<T> Identity { get; }
    bool SameIdentityAs(T other);
}

public class Employee: IEntity<Employee> {
    public virtual Identity<Employee> Identity { get; set; }
    public virtual string Name { get; set; }
}

如何映射此员工?这种方式行不通,我在构建 SessionFactory 时遇到以下异常: 在“Employee”类中找不到属性“Id”的吸气剂

public class EmployeeMap : ClassMap<Employee> {

    public EmployeeMap() {
        Id(x => x.Identity.Id).GeneratedBy.Native();
        Map(x => x.Name);
    }
}

【问题讨论】:

    标签: c# nhibernate fluent-nhibernate fluent-nhibernate-mapping


    【解决方案1】:

    不支持您尝试执行的操作。

    有一个更长的解释,但特别是在使用 DB 生成的 id 时,您应该使用未包装的原生 int 或 long。

    也就是说,您可以将 id 映射为实体中的私有字段,并使用包装器将其公开。这仍然不适用于 LINQ 查询,因此它的价值有限。

    【讨论】:

    • 是否有可能使用另一个 id-strategy 和包装器(Identity 类)?
    • 也许使用assigned(即您的代码负责在保存之前分配它),将包装器映射为复合ID...不过,我认为您遇到了很多麻烦好处可疑的东西。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-09
    • 2011-07-08
    • 2014-12-09
    • 2011-04-10
    • 1970-01-01
    • 2010-12-07
    • 1970-01-01
    相关资源
    最近更新 更多