实体框架:带有未知类型列的通用表中的数据
我是实体框架的新手,在我的项目中,我必须执行通用查询以检索数据库列的最大值。假设这是我的主要方法:
long GetMaxValue(string tableName, string columnName)
{
// code here
}
从字面上看,SQL等效是
SELECT MAX(<columnName>) FROM <tableName>
我创建了一个genericdbentity
类,该类仅包含numericColumn
代表本列的属性,以及在实现的实现中onModeLcreating
我的dbContext
我有:
modelBuilder.Entity<GenericDbEntity>().ToTable(tableName).HasNoKey();
modelBuilder.Entity<GenericDbEntity>().Property(e => e.NumericColumn)
.HasColumnName(columnName)
.HasConversion(p => (?)p, (? p) => Convert.ToInt64(p));
hasconversion
内的问号是因为我实际上不知道该列的DB类型是哪个DB类型(我只知道它是数字),但是我想将其转换为long
,因为那是genericdbentity.numericcolumn
的数据类型。我试图用对象
替换问号,但它说:
属性“ genericdbentity.numericcolumn”是类型为“对象”,当前数据库提供商不支持。要么更改属性CLR类型,要么使用“ [notmapped]''属性或使用“ enmodeLcreating”中的'entityTypebuilder.ignore'忽略属性。
有什么简单的方法可以实现这一目标吗?
PS:我还尝试使用Dynamic
,包括hasConversion
和作为numericColumn
的数据类型,但这无济于事。
编辑:所谓的编译查询是这个:
private static readonly Func<DatabaseContext, long> getMaxValue =
EF.CompileQuery((DatabaseContext context) =>
context.GenericEntity
.Max(e => e.NumericColumn));
I am new to Entity Framework, and in my project I have to perform a generic query to retrieve the maximum value of a db column. Let's say that this is my main method:
long GetMaxValue(string tableName, string columnName)
{
// code here
}
and the SQL equivalent is literally
SELECT MAX(<columnName>) FROM <tableName>
I have created a GenericDbEntity
class which contains only a NumericColumn
property representing this column, and in the implementation of OnModelCreating
inside my DbContext
I have this:
modelBuilder.Entity<GenericDbEntity>().ToTable(tableName).HasNoKey();
modelBuilder.Entity<GenericDbEntity>().Property(e => e.NumericColumn)
.HasColumnName(columnName)
.HasConversion(p => (?)p, (? p) => Convert.ToInt64(p));
The question marks inside HasConversion
are because I don't actually know which is the db type of that column (I only know it's numeric), but I want to convert it to long
, since that's the data type of GenericDbEntity.NumericColumn
. I tried to replace the question marks with object
, but it says:
The property 'GenericDbEntity.NumericColumn' is of type 'object' which is not supported by the current database provider. Either change the property CLR type, or ignore the property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
Is there an easy way to achieve this?
PS: I tried also to use dynamic
, both in HasConversion
and as data type of NumericColumn
, but it didn't help.
Edit: The called compiled query is this one:
private static readonly Func<DatabaseContext, long> getMaxValue =
EF.CompileQuery((DatabaseContext context) =>
context.GenericEntity
.Max(e => e.NumericColumn));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
添加了为特定表和列生成LINQ查询的解决方案。为了使其正常工作,您必须删除
hasConversion
,因为带有转换器的属性在LINQ查询中无法使用。例如,
将生成并执行以下查询:
实现
Added solution which generates LINQ query for particular table and column. For making it working you have to REMOVE
HasConversion
because properties with converters cannot be used in LINQ queries.For example for call
Will be generated and executed the following query:
And implementation