[英]Entity Framework Entity properties as Object type
I have a situation where I have multiple DB versions that I cannot change. 我遇到无法更改的多个数据库版本的情况。 I have readonly MVC application with EF attached to those DB on different environments. 我在EF上将只读MVC应用程序附加到了不同环境中的那些数据库中。 I have encountered a problem when entity property datatypes just don't match and Entity Framework throws datatype mismatch exception. 当实体属性数据类型不匹配并且实体框架引发数据类型不匹配异常时,我遇到了一个问题。 What I've done is created property type of object and mapped a column in a DB then added to original generated column "Passed" and skipped mapping, added getter with object to Int32? 我所做的是创建对象的属性类型,并在数据库中映射了一个列,然后将其添加到原始生成的列“ Passed”中,并跳过了映射,将带有对象的getter添加到Int32? conversion. 转换。 Property "_passed" gets always null sadly. 可悲的是,属性“ _passed”始终为空。
Maybe anyone have an idea how to solve object to Int32 conversion problem or point me to a direction to a more correct approach regarding this issue? 也许有人对如何解决从对象到Int32的转换问题有所了解,或者为我指出了一个解决此问题的更正确方法的方向?
Thanks, E 谢谢,E
[Table("VotingSession")]
public partial class VotingSession
{
[Column("Passed")]
public object _passed;
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int Id { get; set; }
public int Agenda_id { get; set; }
public double? TotalLoggedIn { get; set; }
[NotMapped]
public object Passed {
get
{
if (_passed.GetType() == typeof(bool))
{
return (bool)_passed == true ? 1 : 0;
}
else
{
return _passed;
}
}
}
[NotMapped]
public int? Quorum { get; set; }
I think you can't use backing field for db first models, you can try to do this, Override getter, if typeof the model.Passed is bool, check if true return 1 else 0, if not bool, return this.Passed value; 我认为您不能为数据库第一个模型使用后备字段,您可以尝试执行此操作,覆盖getter,如果模型的类型.passed为bool,请检查是否为true返回1,否则返回0,如果不是bool,则返回this.Passed值;
public object Passed {
get
{
if (this.Passed.GetType() == typeof(bool))
{
return this.Passed == true ? 1 : 0;
}
else
{
return this.Passed;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.