简体   繁体   中英

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. 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? conversion. Property "_passed" gets always null sadly.

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?

Thanks, 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;

public object Passed {
    get
    {
        if (this.Passed.GetType() == typeof(bool))
        {
           return this.Passed == true ? 1 : 0;
        }
        else
        {
            return this.Passed;
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM