System.InvalidCastException:尝试在 C# 中使用 Entity Framework 将对象添加到 SQL 数据库时

System.InvalidCastException: When trying to add an object to a SQL database using Entity Framework in C#

我正在尝试在 C# 中使用 Entity Framework 将对象添加到 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 实体的代码 class:

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; }
}

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

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)
);

我盯着代码看了 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 post 的 link 来验证我是否正确转换了我的值。 https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/sql-server-data-type-mappings?redirectedfrom=MSDN

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

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