Linq Time(7)和TimeSpan Mapping

Meh*_*ran 3 c# linq entity-framework sql-server-2012

我正在尝试在我的表中插入一条记录Test,我正在使用LINQ技术:

问题是time我的表中有一个类型的列,Time(7)但是当我尝试向表中插入数据时,我收到此错误:

Operand type clash: bigint is incompatible with time

这是我test在SQL中的表设计:

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; }
    }
}
Run Code Online (Sandbox Code Playgroud)

在这里我尝试插入我的记录:

    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 !!!!!
    }
Run Code Online (Sandbox Code Playgroud)

我搜索过每一个地方,我发现只是为了映射Time()我应该使用的sql类型TimeSpan,请告诉我我做错了什么!谢谢

War*_*enG 6

ColumnAttribute需要包含DbType参数.设置为

[Column(Storage="_time", DbType="Time NOT NULL")]
Run Code Online (Sandbox Code Playgroud)

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