EF 6 Code-First系列文章目錄:
- 1 翻譯系列:什么是Code First(EF 6 Code First 系列)
- 2.翻譯系列:為EF Code-First設置開發環境(EF 6 Code-First系列)
- 3.翻譯系列:EF Code-First 示例(EF 6 Code-First系列)
- 4.翻譯系列:EF 6 Code-First默認約定(EF 6 Code-First系列)
- 5.翻譯系列:EF 6中數據庫的初始化(EF 6 Code-First 系列)
- 6.翻譯系列:EF 6 Code-First中數據庫初始化策略(EF 6 Code-First系列
- 7.翻譯系列:EF 6中的繼承策略(EF 6 Code-First 系列)
- 8.翻譯系列: EF 6中配置領域類(EF 6 Code-First 系列)
- 9.翻譯系列:EF 6以及EF Core中的數據注解特性(EF 6 Code-First系列)
- 9.1 翻譯系列:數據注解特性之----Table【EF 6 Code-First 系列】
- 9.2 翻譯系列:數據注解特性之---Column【EF 6 Code First系列】
- 9.3 翻譯系列:數據注解特性之Key【EF 6 Code-First 系列】
- 9.4 翻譯系列:EF 6以及 EF Core中的NotMapped特性(EF 6 Code-First系列)
- 9.5 翻譯系列:數據注解之ForeignKey特性【EF 6 Code-First系列】
- 9.6 翻譯系列:數據注解之Index特性【EF 6 Code-First系列】
- 9.7 翻譯系列:EF數據注解特性之--InverseProperty【EF 6 Code-First系列】
- 9.8 翻譯系列:數據注解特性之--Required 【EF 6 Code-First系列】
- 9.9 翻譯系列:數據注解特性之--MaxLength 【EF 6 Code-First系列】
- 9.10 翻譯系列:EF數據注解特性之StringLength【EF 6 Code-First系列】
- 9.11 翻譯系列:數據注解特性之--Timestamp【EF 6 Code-First系列】
- 9.12 翻譯系列:數據注解特性之ConcurrencyCheck【EF 6 Code-First系列】
- 10.翻譯系列:EF 6中的Fluent API配置【EF 6 Code-First系列】
- 10.1.翻譯系列:EF 6中的實體映射【EF 6 Code-First系列】
- 10.2.翻譯系列:使用Fluent API進行屬性映射【EF 6 Code-First】
- 11.翻譯系列:在EF 6中配置一對零或者一對一的關系【EF 6 Code-First系列】
- 12.翻譯系列:EF 6 中配置一對多的關系【EF 6 Code-First系列】
- 13.翻譯系列:Code-First方式配置多對多關系【EF 6 Code-First系列】
- 14.翻譯系列:從已經存在的數據庫中生成上下文類和實體類【EF 6 Code-First系列】
- 15.翻譯系列:EF 6中的級聯刪除【EF 6 Code-First 系列】
- 16.翻譯系列:EF 6 Code -First中使用存儲過程【EF 6 Code-First系列】
- 17.翻譯系列:將Fluent API的配置遷移到單獨的類中【EF 6 Code-First系列】
- 18.翻譯系列:EF 6 Code-First 中的Seed Data(種子數據或原始測試數據)【EF 6 Code-First系列】
- 19.翻譯系列:EF 6中定義自定義的約定【EF 6 Code-First約定】
- 20.翻譯系列:Code-First中的數據庫遷移技術【EF 6 Code-First系列】
- 20.1翻譯系列:EF 6中自動數據遷移技術【EF 6 Code-First系列】
- 20.2.翻譯系列:EF 6中基於代碼的數據庫遷移技術【EF 6 Code-First系列】
- 21.翻譯系列:Entity Framework 6 Power Tools【EF 6 Code-First系列】
MaxLength特性指定了屬性的值所允許的最大值,然后在數據庫中就生成相應列的最大值。MaxLength特性可以應用於實體的String類型的屬性和byte[]數組類型的屬性上。
如果MaxLength特性,應用在其他類型的屬性上就報錯,例如下面的圖中例子:
using System.ComponentModel.DataAnnotations;
public class Student
{
public int StudentID { get; set; }
[MaxLength(50)]
public string StudentName { get; set; }
}
上面代碼例子中,MaxLength(50)應用在StudentName屬性上,指定StudentName的屬性值不能超過50個字符長度。
EF將會檢查標識了MaxLength特性的屬性值,如果長度超過了指定的長度,就會報錯,EF6報錯:System.Data.Entity.Validation.DbEntityValidationException ,EF Core報錯:Microsoft.EntityFrameworkCore.DbUpdateException.
請注意:MaxLength特性,同樣可以用在ASP.NET MVC中,用於驗證屬性的值,了解更多詳情請看這篇文章: Implement Validations in ASP.NET MVC。