簡體   English   中英

Linq Time(7)和TimeSpan Mapping

[英]Linq Time(7) and TimeSpan Mapping

我正在嘗試在我的表中插入名為Test的記錄,我正在使用LINQ技術:

問題是我的表中有一個time列,類型為Time(7)但是當我嘗試向表中插入數據時,我收到此錯誤:

Operand type clash: bigint is incompatible with time

這是我在SQL中的test表設計:

SQL Server 2012中的表'測試'

我在C#中的表實現:

[Table(Name = "Test")]
class TableTest
{
    private int _id;
    [Column(IsPrimaryKey = true, Name = "id", Storage = "_id")]
    public int id
    {
        get { return _id; }
        set { _id = value; }
    }
    private TimeSpan _time;
    [Column(Name = "time", Storage = "_time")]
    public TimeSpan time
    {
        get { return _time; }
        set { _time = value; }
    }
}

在這里我嘗試插入我的記錄:

    DataContext dc = new DataContext(@"Data Source=.;Initial Catalog=DBTest;Integrated Security=True");

    private void button1_Click(object sender, EventArgs e)
    {
        TableTest t = new TableTest();
        t.id = 1;
        t.time = new TimeSpan(7, 30, 0);
        Table<TableTest> t_insert = dc.GetTable<TableTest>();
        t_insert.InsertOnSubmit(t);
        dc.SubmitChanges();  // error Here !!!!!
    }

我搜索過的每一個地方,我發現的只是為了映射Time() sql類型我應該使用TimeSpan ,請告訴我我做錯了什么! 謝謝

ColumnAttribute需要包含DbType參數。 設置為

[Column(Storage="_time", DbType="Time NOT NULL")]

您可以在MSDN上看到更多信息。

暫無
暫無

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

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