使用 FluentNHibernate、SQLite 和 Enum 时遇到的问题
我有一个基于 Sharp Architecture 的应用程序,使用 Fluent NHibernate 和 Automapping。我有以下枚举:
public enum Topics { AdditionSubtraction = 1, MultiplicationDivision = 2, DecimalsFractions = 3 }
和以下类:
public class Strategy : BaseEntity { public virtual string Name { get; set; } public virtual Topics Topic { get; set; } public virtual IList Items { get; set; } }
如果我这样创建该类的实例:
Strategy s = new Strategy { Name = "Test", Topic = Topics.AdditionSubtraction };
它正确保存(感谢这个映射约定:
public class EnumConvention : IPropertyConvention, IPropertyConventionAcceptance { public void Apply(FluentNHibernate.Conventions.Instances.IPropertyInstance instance) { instance.CustomType(instance.Property.PropertyType); } public void Accept(FluentNHibernate.Conventions.AcceptanceCriteria.IAcceptanceCriteria criteria) { criteria.Expect(x => x.Property.PropertyType.IsEnum); } }
但是,在检索时(当 SQLite 是我的数据库时)我收到有关尝试将 Int64 转换为主题的错误。
这在 SQL Server 中工作正常。
有解决方法的想法吗?
谢谢。
I have a Sharp Architecture based app using Fluent NHibernate with Automapping. I have the following Enum:
public enum Topics { AdditionSubtraction = 1, MultiplicationDivision = 2, DecimalsFractions = 3 }
and the following Class:
public class Strategy : BaseEntity { public virtual string Name { get; set; } public virtual Topics Topic { get; set; } public virtual IList Items { get; set; } }
If I create an instance of the class thusly:
Strategy s = new Strategy { Name = "Test", Topic = Topics.AdditionSubtraction };
it Saves correctly (thanks to this mapping convention:
public class EnumConvention : IPropertyConvention, IPropertyConventionAcceptance { public void Apply(FluentNHibernate.Conventions.Instances.IPropertyInstance instance) { instance.CustomType(instance.Property.PropertyType); } public void Accept(FluentNHibernate.Conventions.AcceptanceCriteria.IAcceptanceCriteria criteria) { criteria.Expect(x => x.Property.PropertyType.IsEnum); } }
However, upon retrieval (when SQLite is my db) I get an error regarding an attempt to convert Int64 to Topics.
This works fine in SQL Server.
Any ideas for a workaround?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上,可以将枚举映射到 INT,但在 SQLite 中,INT 将以 Int64 的形式返回,并且由于您无法指定枚举可转换为 long,因此您将收到此错误。我正在使用 NHibernate,所以我的解决方法是创建一个自定义 AliasToBean 类来处理将枚举字段转换为 Int32:
用法:
Actually, it is possible to map enums to INT, but in SQLite, INT will come back as an Int64, and, since you can't specify that your enum is castable to long, you will get this error. I am using NHibernate, so my workaround was to create a custom AliasToBean class that handles converting the enum fields to Int32:
Usage:
默认情况下,emum 会自动映射到 SQLite 的字符串(不知道 SQL Server 会发生什么)。
显然,存储效率较低,但这可能不是问题,除非您有非常大的数据集。
By default, emums are automapped to strings for SQLite (don't know what happens with SQL Server).
Less efficient storage wise, obviously, but that might be a non-issue unless you have really huge data sets.