实体属性¶
模型中的每个实体类型都具有一组属性,这些属性 EF Core 将从数据库中读取和写入数据。 如果使用关系数据库,实体属性将映射到表列。
包含和排除的属性¶
数据注释(data-annotations)
public class User { public int UserId { get; set; } public string Name { get; set; } public DateTime BrithDate {get;set;} [NotMapped] public string FullName {get;set;} }
Fluent Api
protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<User>(b => { b.ToTable("Users"); b.HasKey(u => u.UserId); b.Ignore(u => u.FullName); }); }
列名¶
数据注释(data-annotations)
public class User { [Column("user_id")] public int UserId { get; set; } public string Name { get; set; } public DateTime BrithDate {get;set;} public string FullName {get;set;} }
Fluent Api
protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<User>(b => { b.ToTable("Users"); b.HasKey(u => u.UserId); b.Property(u => u.UserId).HasColumnName("user_id"); }); }
列数据类型¶
使用关系数据库时,数据库提供程序根据属性的 .NET 类型选择数据类型。 它还会考虑其他元数据,如配置的最大长度、属性是否是主键的一部分等。
您还可以配置列,以便为列指定精确的数据类型。 例如,以下代码将配置 Url 为一个非 unicode 字符串,其最大长度为 200 , Rating 小数位数为,小数 5 位数为,小数位数为 2 :
数据注释(data-annotations)
public class User { public int UserId { get; set; } public string Name { get; set; } public DateTime BrithDate {get;set;} [NotMapped] public string FullName {get;set;} [Column(TypeName = "varchar(256)")] public string Url { get; set; } [Column(TypeName = "decimal(5, 2)")] public decimal Rating { get; set; } [Column(TypeName = "blob")] public byte[] Image { get; set; } }
Fluent Api
protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<User>(b => { b.ToTable("Users"); b.Property(u => u.Url).HasColumnType("varchar(256)"); b.Property(u => u.Rating).HasColumnType("decimal(5, 2)"); b.Property(u => u.Image).HasColumnType("blob"); }); }
最大长度¶
配置最大长度会向数据库提供程序提供有关要为给定属性选择的相应列数据类型的提示。 最大长度仅适用于数组数据类型,例如 string 和 byte[].
注意: 神通数据库的 Varchar Varbinary 最大长度均为8000, Text的最大长度为 16777215.如果超出长度,请考虑您的架构设计或者采用大对象, Clob或者Blob.BLOB 称为二进制大对象,用于存储大数据量的二进制数据;CLOB 称为字符大对象,用于存储大数据量的字符数据,它们的最大容量均为 4G.
注意: string 类型的字段默认绑定数据库的clob 类型,为支持最大字符长度,数据库对应列的数据类型应为clob类型,如果使用 varchar () 或者 text 类型,请为应用代码 对应字段属性做长度限制或者绑定列类型, 如 varchar (8000) 对应代码加注释 [StringLength(8000)] 或者[MaxLength(8000)] 或者 Property(u => u.xx).HasMaxLength(8000) , 如果 text 对应代码加注释 Property(u => u.xx).HasColumnType("text") 或者 Property(u => u.xx).HasMaxLength(16777215) 或者 [StringLength(16777215)] 或者[MaxLength(16777215)]同理: byte[] 类型 默认绑定了 blob 数据类型,支持最大字节长度,同样限制长度或者绑定列类型可以使用不同的数据库类型.数据注释(data-annotations)
public class User { public int UserId { get; set; } public string Name { get; set; } public DateTime BrithDate {get;set;} public string FullName {get;set;} [MaxLength(256)] public string Url { get; set; } public decimal Rating { get; set; } }
Fluent Api
protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<User>(b => { b.ToTable("Users"); b.Property(u => u.Url).HasMaxLength(256); }); }
必需属性和可选属性¶
按照约定,.NET 类型包含 null 的属性将配置为可选,而 .NET 类型没有包含 null 的属性将根据需要进行配置。 例如,具有 .net 值类型( int, decimal, bool, DateTime, DateTimeOffset)的属性都是必需的,使用 int? decimal? bool? DateTime? DateTimeOffset? 将配置为可选.
下面的示例显示了一个具有必需和可选属性的实体类型
public class User { public int UserId { get; set; } [Required] public string Name { get; set; } public DateTime? BrithDate {get;set;} }
注意: 神通数据库所有数据类型默认允许值为 NULL, 即字段无声明 NOT NULL,则该字段默认允许值为 NULL,所以在EFcore 实体属性中应该定义该属性为可空类型,除非该字段属性本身是可空类型,如 string.
如下示例:
SQL > CREATE TABLE Products (Id INT AUTO_INCREMENT PRIMARY KEY,Name VARCHAR(128),Licenced BOOLEAN);
SQL语句创建Products表: 其中 字段 Licenced 默认允许其值为NULL;
如果在EFCore 中定义实体类, Product 的属性 Licenced 不是可空类型,那么该实体类将不能与数据库表关联.
下面是错误定义代码:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public bool Licenced { get; set; }
}
正确的定义代码:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public bool? Licenced { get; set; }
}
如果 EFCore 数据类型是可空类型,那么无需用 type + ? 的形式,如上实体类 Product中属性 string 类型 Name 字段.
如果 实际情况 是 字段属性不能为空, 即
public bool Licenced { get; set; }
,那么就要求神舟数据库该表对应字段是不可空类型 (NOT NULL).
SQL > CREATE TABLE Products (Id INT AUTO_INCREMENT PRIMARY KEY,Name VARCHAR(128),Licenced BOOLEAN NOT NULL);
显式配置¶
您可以将可空类型强制限制为不可空类型,如下代码:
数据注释(data-annotations)
public class User { public int UserId { get; set; } [Required] public string Name { get; set; } public DateTime BrithDate {get;set;} }
Fluent Api
protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<User>(b => { b.Property(u => u.Name).IsRequired(); }); }
所以数据库中 Name 列必须是 NOT NULL.