Mis*_*ive 5 c# sql-server entity-framework-core .net-core
考虑这个简单的类,我将使用EF Core 3.1将其用于我的 Domain 对象之一:
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; }
}
}
Run Code Online (Sandbox Code Playgroud)
具有数据库背景,我不想使用默认类型 nvarchar(max) 作为我的数据库 (SQL Server) 中字符串值的列类型。创建表时如何指定EF使用的列类型?
另外,我是否必须包含整个SYSTEM命名空间才能为我的 CreatedDate 字段提供可用的 DateTime 选项,或者还有其他方法吗?
基本上这个问题有两种可能。一种是使用上一个答案中提到的属性或者使用EF core提供的Fluent API。
Fluent API 允许您精确配置数据库属性。
更多信息可以在文档中找到
基本上,数据库上下文中所需的代码如下
modelBuilder.Entity<Address>()
.Property(a => a.StateProvince).HasColumnType("varchar(20)");
Run Code Online (Sandbox Code Playgroud)