從 datetime2 資料類型到 datetime 資料類型的轉換產生一個超出範圍的值,datetime2datetime

來源:互聯網
上載者:User

從 datetime2 資料類型到 datetime 資料類型的轉換產生一個超出範圍的值,datetime2datetime

在使用Entity Framework操作資料庫時遇到的這個錯誤。
這個錯誤比較奇怪,在園子裡看到一位兄弟寫到有位大哥提出的原因是:http://www.cnblogs.com/plwang1990/articles/2025334.html
  sqlServer2008中提供datetime2類型的資料(比datetime更精確),而用entity framework4會預設的把C#中的
System.DateTime類轉化成sqlServer2008中的datetime2類型,而你的資料庫中的元素的屬性設的是datetime類型,
把datetime2轉化成datetime系統會認為會遺失資料,所以會報錯。(感覺這TM是設計缺陷吧!這是熟麼預設設定啊!)
經過調試發現,這位大哥說的當並不完全正確。SqlServer的datetime有效範圍是1753 年 1 月 1 日到 9999 年 12 月 31 日,如果超出這個範圍,EF就會把datetime轉換為datetime2,也就是會出現上面的錯誤。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.