EF中的动态属性绑定? [英] Dynamic Property Binding in EF?

查看:101
本文介绍了EF中的动态属性绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显然,相同数据库实体(表)的相同列的值类型因环境而异。他们拒绝更新为通用类型-不要问为什么!

Apparently, the same columns value-type differs across environments for the same database entity (table) & they refuse to update to a common type - don't ask why!

我正在将Entity Framework(版本6.1.3)与工作单元一起用于数据访问。而且,如您所料,由于DEV& QA数据库定义与同一列不匹配。

I am using Entity Framework (version 6.1.3) alongside a Unit of Work for data-access. And, as you can guess, I am getting errors because the DEV & the QA database definitions do not match for the same column.

好消息:

我们不保存

THE GOOD NEWS:
We do not save into these particular tables - we only query those particular tables.

样本模型:

显然有更多的列了。

SAMPLE MODEL:
There are obviously more columns than this.

public partial class Transactions
{
    [Key]
    public int TransactionId { get; set; }

    public float Amount { get; set; } //<-- This type differs between database environments
}

我的问题:


  • 是否可以在Entity Framework中动态绑定列的值?

  • 或者,我可以将其视为动态的引擎盖...并将其转换为对我的模型恒定的预期类型吗?

最佳-并要清除:

我想具体定义属性,并让实体框架从未知类型& amp;转换;

OPTIMALLY - AND TO BE CLEAR:
I would like to define the property concretely, and have Entity Framework "convert" from the unknown type & into the concrete type - but under-the-hood.

我们将不胜感激。

推荐答案

如果列的类型兼容(即都是数字),则可以在类上使用通用类型,然后禁用模型检查(和迁移)。此解决方案可以在某些DBMS上工作,而不能在某些其他DBMS上工作(取决于提供程序)。

If the types of the columns are compatible (i.e. are all numbers) you can have a common type on the class then disable model checking (and migration). This solution could work on some DBMSs and not on some Others (depends on the provider).

您可以编写带有视图的视图并将其映射到模型中(而不是

You can write a view with cast and map it in your model (and not the table).

您可以使用EF(官方文档 https://msdn.microsoft.com/en-us/data/jj592907.aspx 与Dapper解决方案类似,具有相同的优点和缺点,在您的情况下,您会获得延迟加载)。

You can write a direct query with EF (official docs https://msdn.microsoft.com/en-us/data/jj592907.aspx similar to Dapper solution with same advantages and defects, in your case lazy load won't work).

这篇关于EF中的动态属性绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆