[英]Searching in sql server json column and consume it using entity framework core
[英]Specifying the column type when using entity framework core 3.1 with SQL Server
考慮這個簡單的 class,我將在EF Core 3.1中將其用於我的域對象之一:
using System;
namespace Blah.Domain
{
public class Response
{
public int Id { get; set; }
public string FullResponseText { get; set; }
public string HttpResponseCode { get; set; }
public string TransactionId { get; set; }
public string TransactionType { get; set; }
public DateTime CreatedDate { get; set; }
}
}
具有數據庫背景,我不想使用默認類型 nvarchar(max) 作為數據庫 (SQL Server) 中字符串值的列類型。 創建表時如何指定 EF 使用的列類型?
另外,我是否必須包含整個SYSTEM
命名空間才能讓我的 CreatedDate 字段可以使用 DateTime 選項,還是有其他方法?
您應該能夠在每個字符串值上添加一個屬性,如下所示
[MaxLength(50)] //Whatever int value in `maxlength` will be the size in sql
public string FullResponseText { get; set; }
[MaxLength(255)]
public string HttpResponseCode { get; set; }
etc.....
或者你可以使用[StringLength(50, MinimumLength = 5)]
要使用[MaxLength()]
,您需要System.Collections.Generic
。 對於DateTime
System
應該是您需要的唯一namespace
。
這個問題基本上有兩種可能性。 一種是使用前面回答中提到的屬性或者使用EF core提供的fluent API。
Fluent API 允許您精確配置數據庫屬性。
更多信息可以在文檔中找到
基本上所需的代碼是數據庫上下文中的以下代碼
modelBuilder.Entity<Address>()
.Property(a => a.StateProvince).HasColumnType("varchar(20)");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.