Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using NVarChar in asp.net core

I'm creating a model for an existing database. How do I use nvarchar(max) as an attribute to my property? Do I use an extension to my attribute? Or is it entirely different.

The SQL Server database is using a datatype of nvarchar(max).

[MaxLength + ???]
public string Bucket { get; set; }
like image 632
Kate Avatar asked Sep 04 '19 14:09

Kate


People also ask

How do I use NVARCHAR?

The syntax for declaring the nvarchar variable is nvarchar(n), where n defines the string size in byte-pairs. The value of n must be from 1 through 4000. For Example, when we store a string of length 10, The string will occupy (10*2) + 2 (=22 bytes) bytes of storage.

What can be stored in NVARCHAR?

nvarchar [ ( n | max ) ] max indicates that the maximum storage size is 2^30-1 characters (2 GB). The storage size is two times n bytes + 2 bytes. For UCS-2 encoding, the storage size is two times n bytes + 2 bytes and the number of characters that can be stored is also n.

What is the datatype with NVARCHAR?

The NVARCHAR data type stores character data in a variable-length field. Data can be a string of single-byte or multibyte letters, digits, and other characters that are supported by the code set of your database locale.

What is NVARCHAR in C#?

Nvarchar is a string, if you want to turn it in to a int you will have to go through some kind of conversion as I could have "Potato" in the column, what int would that map to? Here is a listing of all of the SQL types and what C# types they map to.


Video Answer


2 Answers

Try this:

Column(TypeName = "nvarchar(MAX)")]
public string Bucket { get; set; }
like image 179
JOSEFtw Avatar answered Oct 25 '22 02:10

JOSEFtw


You can use the attribute as suggested by @JOSEFtw but it's also possible to do with the fluent API if that's how you're defining other properties.

modelBuilder.Entity<YourType>()
                    .Property(p => p.Bucket)
                    .HasColumnType("nvarchar(max)");
like image 24
SBFrancies Avatar answered Oct 25 '22 01:10

SBFrancies