Linq Time(7)和TimeSpan Mapping [英] Linq Time(7) and TimeSpan Mapping

查看:111
本文介绍了Linq Time(7)和TimeSpan Mapping的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的表中插入一个记录,名为 Test ,我使用LINQ技术:



问题是我的表格中有一个时间列,类型为 Time(7),但是当我尝试将数据插入到表中我得到这个错误:



操作数类型冲突:bigint与时间不兼容



这是我的测试表在SQL中设计:





我的在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; }
}
}

这里我尝试插入我的记录: / p>

  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);
表< TableTest> t_insert = dc.GetTable< TableTest>();
t_insert.InsertOnSubmit(t);
dc.SubmitChanges(); // error Here !!!!!
}

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

解决方案

您的 ColumnAttribute 需要包含 DbType 参数。将其设置为

  [Column(Storage =_ time,DbType =Time NOT NULL)] 

您可以在 MSDN


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

解决方案

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

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

You can see more at MSDN.

这篇关于Linq Time(7)和TimeSpan Mapping的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆