简体   繁体   中英

What happens if I don't specify StringLength or MaxLength on a string property?

Right now I'm creating the models for a database with the code-first approach. It will be for a really big data (like millions of rows). So, I'm trying to be very careful about the types I chose. Using shorts, bytes and enums wherever possible.

Now, StringLength and MaxLength annotations are both limiting the maximum allowed length of a string (or an array for the latter).

So, do they designate the number of bytes database is reserving for that field? If so, how much bytes are reserved when they hadn't been specified? Or are they merely for validation purposes and nothing more?

I'm not even sure if the reserved space in database is relevant to the type I choose. Maybe the inside logic is totally different.

When I think about it, another question pops in my head. What happens then, if I add an "Article" column with StringLength(1000)? Considering one char occupies 2 bytes, is it now reserving 2kB for each field? Or is it totally unrelated to that?

This behavior depends on various parameters. First on your databases like SQL Server or MySQL and then it depends on collation also like utf8, latin1 and all.

I have run both the cases on my MySQL DB, here is the result.

Model:

public virtual string field1 { get; set; }

[StringLength(1000)]
public virtual string field2 { get; set; }

Generated Migration:

protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.AddColumn<string>(
        name: "field1",
        table: "Category",
        nullable: true);

    migrationBuilder.AddColumn<string>(
        name: "field2",
        table: "Category",
        maxLength: 1000,
        nullable: true);
}

Table:

在此处输入图片说明

So it has created a VARCHAR(1000) and LONGTEXT fields. Again the length of data it contains totally depends on collation/encoding of the field.

LONGTEXT size: 4,294,967,295 = (2^32−1) bytes = 4 GiB

LONGTEXT can contain 4,294,967,295 bytes of data. UTF-8 contains multi-byte characters. Therefore, if you filled the field using only the Danish character "Ø", you would only get 2,147,483,647 characters, as that UTF-8 character is composed of two bytes. If you filled it with "a", you would get 4,294,967,295 characters.

if you don't specify StringLength or MaxLength on a string property,.net won't validate anything but it database will throw an exception if constraint is violated. Ex:- size of col1 is 100 in db, and you pass a value of size 200, it will throw a db exception.

When you declare a column with size 1000 in db, when it will be fetched programatically, the size will be 2 kb for a single field unless you apply trim while querying.

Attributes are used for declaration, information and validation purposes mostly. An attribute is a declarative tag that is used to convey information to runtime about the behaviors of various elements like classes, methods, structures, enumerators, assemblies etc. in your program. You can add declarative information to a program by using an attribute. A declarative tag is depicted by square ([ ]) brackets placed above the element it is used for.

Attributes are used for adding metadata, such as compiler instruction and other information such as comments, description, methods and classes to a program. The .Net Framework provides two types of attributes: the pre-defined attributes and custom built attributes.

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