简体   繁体   中英

Changing ntext to nvarchar using C# and Entity Framework

I have to add sorting to existing database created in SQL Server. The problem is that this database contains ntext columns that are not supported by LinQ's OrderBy method. The database was written in a code-first approach, so I have access to template of database, but I can't look at ready database working on the server.

I've tried to change string type properties marking them as

[Column(TypeName = "nvarchar(MAX)")]

but then I got a

Sequence contains no matching element

exception which I don't know how to fix.

This is the way that I wanted to sort my data(i got exception right in the below instruction:

MyDatabase.MyTable.OrderBy(x => x.MyRow).Load();

Before I changed TypeName to nvarchar , I've got this error:

Large objects (ntext and image) cannot be used in ORDER BY clauses

Can somebody help me with fixing things up to make possible to sort data from database?

I'll appreciate any kind of help. Thanks in advance!

In T-SQL you can solve this in many ways, may be you can adopt one of them for you too?

  1. ORDER BY (cast MyTextCol as nvarchar(max))

  2. create a view from this table with that field casted as nvarchar(max) and use it instead of your table(even in future)

  3. ALTER TABLE myTable ALTER COLUMN myTextCol nvarchar(max) The last one solves your problem and takes no time to be made: it's just a metadata operation, nothing will reorganizen in your table for existing rows

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM