簡體   English   中英

System.InvalidCastException:嘗試在 C# 中使用實體框架將對象添加到 SQL 數據庫時

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

我正在嘗試使用 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)
);

在盯着代碼 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

我認為文本框中的這些字段也是如此,也許您必須獲取文本值:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM