C# 具有枚举属性的Nhibernate查询实体
我需要使用enum属性查询实体C# 具有枚举属性的Nhibernate查询实体,c#,hibernate,nhibernate,C#,Hibernate,Nhibernate,我需要使用enum属性查询实体PersonTargetBookingSystemType public class Person : EntityWithTypedId<PersonCompositeId> { public virtual string Key { get; set; } public virtual TargetBookingSystemType TargetBookingSystemType { get; set; } } public
Person
TargetBookingSystemType
public class Person : EntityWithTypedId<PersonCompositeId>
{
public virtual string Key { get; set; }
public virtual TargetBookingSystemType TargetBookingSystemType { get; set; }
}
public class PersonMap : ClassMap<Person>
{
public PersonMap()
{
this.CompositeId(x => x.Id).KeyProperty(y => y.AccountName, "[AccountName]").KeyProperty(y => y.Domain, "[Domain]");
this.Table("Person");
this.Map(x => x.Key).Column("[Key]");
this.Map(x => x.TargetBookingSystemType).Column("[TargetBookingSystemType]");//.CustomType<TargetBookingSystemType>();
}
}
public enum TargetBookingSystemType
{
GoogleCalendarAPIv3 = 1,
MSExchange2007 = 2,
MSExchange2010 = 3,
MSExchange2013 = 4,
MSOnline = 5
}
CREATE TABLE [dbo].[Person](
[Domain] [varchar](3) NOT NULL,
[AccountName] [varchar](255) NOT NULL,
[Key] [varchar](255) NOT NULL,
[TargetBookingSystemType] [nvarchar](20) NULL
)
如何在结果中也获得enum属性 我认为NH在处理enum
类型时没有什么特别的东西?()
一旦列的基础数据类型正常(基于@Jehof的评论),我希望这个查询就足够了:
var results = session.Query<Person>().Where(p => p.TargetBookingSystemType == TargetBookingSystemType.MSExchange2010).ToList();
var results=session.Query();
我不认为NH有什么特别的东西可以处理enum
类型?()
一旦列的基础数据类型正常(基于@Jehof的评论),我希望这个查询就足够了:
var results = session.Query<Person>().Where(p => p.TargetBookingSystemType == TargetBookingSystemType.MSExchange2010).ToList();
var results=session.Query();
当您需要查询数据库中具有enum属性和nvarchar列的实体时,没有解决方案
可能的解决办法
将属性的数据类型从enum更改为string,并将nvarchar保留在DB中
public class Person : EntityWithTypedId<PersonCompositeId>
{
public virtual string Key { get; set; }
public virtual string TargetBookingSystemType { get; set; }
}
当您需要查询数据库中具有enum属性和nvarchar列的实体时,没有解决方案 可能的解决办法 将属性的数据类型从enum更改为string,并将nvarchar保留在DB中
public class Person : EntityWithTypedId<PersonCompositeId>
{
public virtual string Key { get; set; }
public virtual string TargetBookingSystemType { get; set; }
}
您应该使用smallint作为列的类型,因为枚举是按其基础类型(int)存储的。此外,如果您选择允许NULL作为值,则应将属性TargetBookingSystemType声明为可空。@Jehof感谢您的注意!考虑到和的可能重复,它无法解决我的问题。您应该使用smallint作为列的类型,因为枚举是按其基础类型(int)存储的。此外,如果您选择允许NULL作为值,则应将属性TargetBookingSystemType声明为可空。@Jehof感谢您的注意!它不会解决我的问题,尽管可能重复的和。嗨,请检查更新的答案。您的答案也是可能的解决方案,但我希望避免更改DB中的类型,因为生产atm中有更多的应用程序,我无法确保我不会通过修改DB来破坏其他内容。@请编辑您的问题以反映这一点。为此,只需调整问题,以流畅地给出您的所有需求,而无需读者从几个部分中找出它们。您好,请检查更新的答案。您的答案也是可能的解决方案,但我希望避免更改DB中的类型,因为生产atm中有更多的应用程序,我无法确保我不会通过修改DB来破坏其他内容。@请编辑您的问题以反映这一点。为此,只需调整问题,以流畅地给出您的所有需求,而无需读者从几个部分中检索它们。