简体   繁体   English

如果我没有在字符串属性上指定StringLength或MaxLength会发生什么?

[英]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). 现在,StringLength和MaxLength注释都限制了字符串(或后者的数组)的最大允许长度。

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)? 如果我使用StringLength(1000)添加“文章”列,会发生什么情况? Considering one char occupies 2 bytes, is it now reserving 2kB for each field? 考虑到一个char占用2个字节,是否现在为每个字段保留2kB? 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. 首先在您的数据库(如SQL Server或MySQL)上,然后取决于排序规则(如utf8,latin1等)。

I have run both the cases on my MySQL DB, here is the result. 我已经在MySQL数据库上运行了这两种情况,这就是结果。

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. 因此,它创建了一个VARCHAR(1000)LONGTEXT字段。 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大小:4,294,967,295 =(2 ^ 32-1)字节= 4 GiB

LONGTEXT can contain 4,294,967,295 bytes of data. LONGTEXT可以包含4,294,967,295字节的数据。 UTF-8 contains multi-byte characters. UTF-8包含多字节字符。 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. 因此,如果仅使用丹麦字符“Ø”填充该字段,则将仅获得2,147,483,647个字符,因为该UTF-8字符由两个字节组成。 If you filled it with "a", you would get 4,294,967,295 characters. 如果用“ a”填充,则将得到4,294,967,295个字符。

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. 如果未在字符串属性上指定StringLength或MaxLength,则.net不会验证任何内容,但如果违反约束,则数据库将引发异常。 Ex:- size of col1 is 100 in db, and you pass a value of size 200, it will throw a db exception. 例如:col1的大小在db中为100,并且您传递的大小为200,它将抛出db异常。

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. 当您声明以db为单位的大小为1000的列时,将以编程方式获取该列时,单个字段的大小将为2 kb,除非您在查询时应用trim。

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. .Net Framework提供两种类型的属性:预定义属性和自定义生成的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果我没有使用CSharpCodeProvider指定CompilerVersion,为什么大多数样本都指定它,会发生什么? - What happens if I don't specify CompilerVersion with CSharpCodeProvider and why do most samples specify it? string[] 或 List 的 DataAnnotations MaxLength 和 StringLength<string> ? - DataAnnotations MaxLength and StringLength for string[] or List<string>? 如果我不调用dispose()会发生什么? - What happens if i don't call dispose()? 如果我不等待任务会怎样? - What happens if I don't await a task? 如果我不在笔对象上调用Dispose会发生什么? - What happens if I don't call Dispose on the pen object? 如果我不实现部分方法会怎样? - what happens if I don't implement a partial method? 当我不向[DataContract]中的字段添加[DataMember]时会发生什么? - What happens when I don't add [DataMember] to a field in [DataContract]? 如果我不处置实体框架上下文会发生什么? - What happens, if I don't dispose the Entity Framework context? 当我将XsltParameter传递给XSLT但未在XSLT中定义时会发生什么? - What happens when I pass an XsltParameter to an XSLT but don't define it in the XSLT? 如果我没有在C#控制台应用程序中关闭System.Diagnostics.Process,该怎么办? - What happens if I don't close a System.Diagnostics.Process in my C# console app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM