-->

Linq Time(7) and TimeSpan Mapping

2020-07-23 03:37发布

问题:

I'm trying to insert a record to my table called Test , I'm using the LINQ technique :

The problem is I have a time column in my table with type of Time(7) but when I try to insert data to table I get this error :

Operand type clash: bigint is incompatible with time

this is my test table Design in SQL :

My table Implementation in 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; }
    }
}

and here I try to Insert my Record :

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

I've searched every where , All I found was that for mapping Time() sql type I should use TimeSpan , please Tell me What I'm doing wrong ! thanks

回答1:

Your ColumnAttribute needs to include the DbType parameter. Set it to

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

You can see more at MSDN.