System.InvalidCastException:当尝试使用C#中的实体框架将对象添加到SQL数据库时

我试图在C#中使用实体框架将对象添加到SQL数据库。数据来自文本框和日期选择器控件的组合。我猜测这是我解析数据时遇到的问题,但是错误对我来说似乎很模糊。任何建议将不胜感激。

这是将数据添加到数据库的代码。

private void new_btn_Click(object sender,RoutedEventArgs e)
{
        var load = new Load
        {
            pro_num = pro_txt.Text,quote_num = quote_txt.Text,ref_num = ref_txt.Text,weight = Convert.ToDouble(weight_txt.Text),pieces = Convert.ToInt32(pieces_txt.Text),commodity = commodity_txt.Text,mileage = Convert.ToDouble(mileage_txt.Text),carrier_rate = Convert.ToDecimal(carrierRate_txt.Text),customer_rate = Convert.ToDecimal(customerRate_txt.Text),pick_appointment = pickDate_picker.SelectedDate,drop_appointment = dropDate_picker.SelectedDate,driver_id = Convert.ToInt32(driver_txt),dispatch_id = Convert.ToInt32(dispatch_txt),customer_id = Convert.ToInt32(customer_txt),broker_id = Convert.ToInt32(broker_txt),};

        Hotloadmodel.Loads.Add(load);
        Hotloadmodel.SaveChangesAsync();
}

以下是EF实体类的代码:

public partial class Load
{
    public int bol_num { get; set; }
    public string pro_num { get; set; }
    public string quote_num { get; set; }
    public string ref_num { get; set; }
    public Nullable<double> weight { get; set; }
    public Nullable<int> pieces { get; set; }
    public string commodity { get; set; }
    public Nullable<double> mileage { get; set; }
    public Nullable<decimal> carrier_rate { get; set; }
    public Nullable<decimal> customer_rate { get; set; }
    public Nullable<System.DateTime> pick_appointment { get; set; }
    public Nullable<System.DateTime> drop_appointment { get; set; }
    public Nullable<int> driver_id { get; set; }
    public Nullable<int> dispatch_id { get; set; }
    public Nullable<int> customer_id { get; set; }
    public Nullable<int> broker_id { get; set; }
}

这是我的数据库表的架构。

CREATE TABLE [dbo].[Loads] 
(
    [bol_num]          INT IDENTITY (1,1) NOT NULL,[pro_num]          VARCHAR(20)  NULL,[quote_num]        VARCHAR(20)  NULL,[ref_num]          VARCHAR(20)  NULL,[weight]           FLOAT(53)    NULL,[pieces]           INT          NULL,[commodity]        VARCHAR(20)  NULL,[mileage]          FLOAT(53)    NULL,[carrier_rate]     MONEY        NULL,[customer_rate]    MONEY        NULL,[pick_appointment] SMALLDATETIME NULL,[drop_appointment] SMALLDATETIME NULL,[driver_id]        INT           NULL,[dispatch_id]      INT           NULL,[customer_id]      INT           NULL,[broker_id]        INT           NULL,PRIMARY KEY CLUSTERED ([bol_num] ASC)
);
rayest 回答:System.InvalidCastException:当尝试使用C#中的实体框架将对象添加到SQL数据库时

盯着代码15分钟后,我意识到我忽略了在这些行上指定.Text属性

        driver_id = Convert.ToInt32(driver_txt),dispatch_id = Convert.ToInt32(dispatch_txt),customer_id = Convert.ToInt32(customer_txt),broker_id = Convert.ToInt32(broker_txt),

此外,我使用了相关SO帖子中的此链接来验证我是否正确转换了我的值。 https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/sql-server-data-type-mappings?redirectedfrom=MSDN

,

我认为文本框中的这些字段也是如此,也许您必须获取文本值:

    public function tags()
    {
        return $this->morphToMany(Tag::class,'model','model_has_tags');
    }
本文链接:https://www.f2er.com/3017487.html

大家都在问