繁体   English   中英

使用Fluent NHibernate从Identity类映射ID

[英]Map an Id from an Identity class with Fluent NHibernate

我正在尝试使用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);
    }
}

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

有更长的解释,但是,特别是在使用数据库生成的ID时,应使用未包装的本机int或long。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM