問題描述
這是我的代碼
ProductController.cs
public ActionResult Details(string id)
{
product productx = productDB.products.Single(pr => pr.Product1 == id);
return View(productx);
}
Details.aspx
<td>
<%-- : Html.ActionLink("Edit", "Edit", new { id=item.Id }) % -->
<%: Html.ActionLink("Details", "Details", new { id = item.Product1 })%>
</td>
這是我用來從 sql 數(shù)據(jù)庫(kù)中列出一些產(chǎn)品的方法,每個(gè)產(chǎn)品都有一個(gè)指向詳細(xì)信息頁(yè)面的鏈接以顯示有關(guān)它的更多信息
this is what im using to list some products from a sql database, each product have a link to a Details page to show more informations about it
我試圖只將產(chǎn)品標(biāo)簽放在該鏈接中,讓它顯示類似www.mysite.comproductsattery(不是ID)
what Im trying is to only put the product label in that link to let it show something like www.mysite.comproductsattery (not the id)
我認(rèn)為這應(yīng)該可行,但它會(huì)拋出一個(gè)數(shù)據(jù)類型 text 和 nvarchar 在等于運(yùn)算符中不兼容. 錯(cuò)誤和(pr => pr.Product1.Equals(id));
都不起作用
I've imagined this should work, but it throw an The data types text and nvarchar are incompatible in the equal to operator. error and
neither (pr => pr.Product1.Equals(id));
works
錯(cuò)誤很明顯,我問我應(yīng)該怎么做才能讓它以這種方式工作?
the error is clear and Im asking how should I do to make it work this way ?
謝謝
推薦答案
TEXT
TEXT columns in SQL Server are considered Large Object data and therefore aren't indexable/searchable. They're also deprecated. So, actually, the problem is in your database, not in your application.
如果將列類型更改為 varchar(max)
,則可以存儲(chǔ)相同數(shù)量的字符數(shù)據(jù),但應(yīng)該不會(huì)出現(xiàn)此問題.然后,將您的 Linq 更新為 SQL 實(shí)體,您將不再遇到此特定錯(cuò)誤.
If you change the column type to a varchar(max)
, you can store the same amount of character data but shouldn't have this problem. Then, update your Linq to SQL entity, and you'll no longer get this particular error.
話雖如此……名為ID
的列不應(yīng)該是TEXT
或 varchar(max)
,它應(yīng)該是一個(gè)自動(dòng)遞增的整數(shù) ID 或 GUID (uniqueidentifier
),因此您可能需要重新訪問您的數(shù)據(jù)庫(kù)設(shè)計(jì).但假設(shè)您有充分的理由將 ID 設(shè)為任意大小的字符串值,上述更改將允許您對(duì)列進(jìn)行過濾.
Having said that... a column named ID
shouldn't be TEXT
or varchar(max)
, it should be an auto-increment integer ID or a GUID (uniqueidentifier
), so you might want to revisit your DB design. But assuming you have good reasons for IDs to be string values of arbitrary size, the above change will allow you to filter on the column.
這篇關(guān)于數(shù)據(jù)類型 text 和 nvarchar 在等于運(yùn)算符中不兼容的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!