在EntityFramework十进制属性

时间:2015-06-20 02:47:33

标签: entity-framework decimal

我对EF很新,并试图在REST风格中创建一个UPDATE方法,该方法将在对象中获取一个或多个属性,并仅使用我传入的数据更新数据库。

我有以下代码适用于多种类型。但是,我最近添加了System.Decimal,并且收到错误,我无法使用Property()方法,因为我的十进制字段不是原始类型或复杂类型。错误发生在以下行:

 pt.Property(propertyInfo.Name).IsModified = true;

实际的错误消息是:

附加信息:“预约”类型的属性“里程”不是原始属性或复杂属性。 Property方法只能与原始或复杂属性一起使用。使用Reference或Collection方法。

我的属性“Miles”是一个18.2的十进制小数,在EF类和十进制的EF类以及我的数据模型类。

我花了两天时间寻找任何线索或解决方案,但无处可去。帮助我obi wan kenobi,你是我唯一的希望......

    [ResponseType(typeof(void))]
    public async Task<IHttpActionResult> PutAppointment(int id,[FromBody] DTO.Appointment appointment)
    {
        // Check for invalid model and mis-matched ID
        if (!ModelState.IsValid){ return BadRequest(ModelState); }
        if (id != appointment.AppointmentID) { return BadRequest(); }

        // Create and populate an Appointment Entity which we will use for saving our data later
        Appointment tempAppt = new Appointment();
        tempAppt.AppointmentID = appointment.AppointmentID;
        db.Appointments.Attach(tempAppt);
        var pt = db.Entry(tempAppt);

        // Loop through all the properties on the object that was passed into this method
        //  and update the Entity with any that were provided.
        foreach (PropertyInfo propertyInfo in appointment.GetType().GetProperties())
        {
            if (propertyInfo.CanRead)
            {
                switch (propertyInfo.PropertyType.ToString())
                {
                    case "System.Int32":
                        if ((int)propertyInfo.GetValue(appointment) != -1)
                        {
                            pt.Property(propertyInfo.Name).CurrentValue = (int)propertyInfo.GetValue(appointment);
                            pt.Property(propertyInfo.Name).IsModified = true;
                        }
                        break;
                    case "System.Decimal":
                        if ((decimal)propertyInfo.GetValue(appointment) != -1)
                        {
                            propertyInfo.SetValue(appointment, (decimal)propertyInfo.GetValue(appointment));
                            pt.Property(propertyInfo.Name).IsModified = true;
                        }
                        break;
                    case "System.String":
                        if ((string)propertyInfo.GetValue(appointment) != "NONE")
                        {
                            pt.Property(propertyInfo.Name).CurrentValue = (string)propertyInfo.GetValue(appointment);
                            pt.Property(propertyInfo.Name).IsModified = true;
                        }
                        break;
                    case "System.DateTime":
                        if ((DateTime)propertyInfo.GetValue(appointment) != new DateTime(2099, 1, 1))
                        {
                            pt.Property(propertyInfo.Name).CurrentValue = (DateTime)propertyInfo.GetValue(appointment);
                            pt.Property(propertyInfo.Name).IsModified = true;
                        }
                        break;
                    case "System.TimeSpan":
                        if ((TimeSpan)propertyInfo.GetValue(appointment) != new TimeSpan(0))
                        {
                            pt.Property(propertyInfo.Name).CurrentValue = (TimeSpan)propertyInfo.GetValue(appointment);
                            pt.Property(propertyInfo.Name).IsModified = true;
                        }
                        break;
                    //case "System.Nullable`1[System.Guid]":
                    case "System.Guid":
                        if ((Guid)propertyInfo.GetValue(appointment) != new Guid("00000000-0000-0000-0000-000000000000"))
                        {
                            pt.Property(propertyInfo.Name).CurrentValue = (Guid)propertyInfo.GetValue(appointment);
                            pt.Property(propertyInfo.Name).IsModified = true;
                        }
                        break;
                }
            }
        }

1 个答案:

答案 0 :(得分:2)

像往常一样,当我遇到需要花费超过几个小时才能解决的问题时,可归结为愚蠢的事情。

我的数据/业务对象在“Miles”上使用大写“M”,而我的数据库(以及因此EF)在“里程”上使用小写“m”。一旦我放弃并将其更改为我工作的数据类型,很明显它不是问题的类型。然后我只需要盯着它看足够长的时间来注意案件问题。

你是......