五月综合缴情婷婷六月,色94色欧美sute亚洲线路二,日韩制服国产精品一区,色噜噜一区二区三区,香港三级午夜理伦三级三

您現(xiàn)在的位置: 365建站網(wǎng) > 365文章 > Castle ActiveRecord 在查詢時會自行更新數(shù)據(jù)庫數(shù)據(jù)的問題

Castle ActiveRecord 在查詢時會自行更新數(shù)據(jù)庫數(shù)據(jù)的問題

文章來源:365jz.com     點擊數(shù):431    更新時間:2009-09-19 10:44   參與評論

    在使用AR過程中發(fā)現(xiàn)在查詢數(shù)據(jù)時,有時會在后臺出現(xiàn)數(shù)據(jù)庫的update操作,對非字符串類型的字段,如果值為空,則更新為一個較小的值,如,int類型字段,自動更新為0,bit類型字段,自動更新為假。
    發(fā)現(xiàn)這個問題時,就分析是空屬類型的問題,但又覺得不應(yīng)該啊,update操作應(yīng)該出現(xiàn)在對數(shù)據(jù)的更新或插入操作時,為什么會在查詢時后臺生成update語句來處理數(shù)據(jù)庫呢?從網(wǎng)上查了一下資料,沒有發(fā)現(xiàn)相關(guān)討論,無意中發(fā)現(xiàn)了下面的這篇文章,覺得是類似的問題,只不過他遇到的是DateTime類型而已,就貼在了下面。
    經(jīng)過把實體類的非字符串型的屬性改為空屬類型,就沒有上述問題了。如:int?,bool?等。
    下面是看來的討論類似問題的文章,關(guān)鍵的部分是最后一段。

NHibernate and the SqlTypeException

轉(zhuǎn)自http://blog.jagregory.com/2007/12/18/nhibernate-and-the-sqltypeexception/

NHibernate is a wonderful piece of technology, I love it probably more than is reasonable for code. It does however, occasionally scare you with some seemingly odd behavior. I say seemingly, because every time I’ve had trouble it’s actually ended up being my own fault. This is one of those times.

Picture a simple page, with a DeleGrid control, being bound using NHiberate. Baring in mind how the DeleGrid works, two queries were being executed, one to return the first page of data and another to get the total row-count for the grid. These queries were identical apart from the paging in one, and the projection in the other.

Upon execution of the second query, NHibernate was throwing a SqlTypeException for a SqlDateTime overflow. SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM. This was pretty bizarre. Why on earth would the first query succeed (and bring back records, fully populated), but the same query again would die.

A good place to start for NHibernate debugging is always the logs, so I delved in. I discovered NHibernate was attempting to execute an update statement just before it tried the second query. It just kept getting stranger, why would a straightforward query cause an update?

I thought i’d investigate why the update statement was failing first, then I’d tackle the problem of why it was even updating at all. Looking at the query I identified the column that was causing the exception, it was (as expected) a DateTime column that was trying to be set to DateTime.MinValue. This exception is thrown because .Net and SQL Server have different ideas over what the minimum value for a DateTime should be.

Now, why would this column be being set at all? Well, it ends up that the column in the database was nullable, but the property in the object wasn’t. So because DateTime is a value type and cannot be set to null, NHibernate was populating it with the closest value to null as it could manage.

That was the key, as soon as I had that realisation, it was obvious what the problem was.

NHibernate knew that the database had a nullable column, but it had to manage with the non-nullable field on the object. When it came to run the second query, it noticed that the property wasn’t null as the mapping file said it should be, so it determined the value must have changed. It then attempted to persist those changes before executing the query!

To break it down

  1. Nullable column pulled into a non-nullable field forces NHibernate to create the smallest value it can.
  2. NHibernate then checks for any changes, expecting a null on that field but finding a value.
  3. Object now considered dirty because value has allegedly changed.
  4. NHibernate performs an update before it pulls back the data agian.

So the fix was simply to make the DateTime in the object a DateTime?, a nullable DateTime. That got rid of the false update, and fixed my queries. Simple when you know what the problem is.

So the moral of the story is: Make sure everything is in sync - schema, mappings and POCOs.

Tag標(biāo)簽: Castle

如對本文有疑問,請?zhí)峤坏浇涣髡搲?,廣大熱心網(wǎng)友會為你解答??! 點擊進入論壇

發(fā)表評論 (431人查看0條評論)
請自覺遵守互聯(lián)網(wǎng)相關(guān)的政策法規(guī),嚴(yán)禁發(fā)布色情、暴力、反動的言論。
昵稱:
最新評論
------分隔線----------------------------

其它欄目

· 建站教程
· 365學(xué)習(xí)

業(yè)務(wù)咨詢

· 技術(shù)支持
· 服務(wù)時間:9:00-18:00
365建站網(wǎng)二維碼

Powered by 365建站網(wǎng) RSS地圖 HTML地圖

copyright © 2013-2024 版權(quán)所有 鄂ICP備17013400號