[英]Entity Framework Code First - determining data type
I'm learning Entity Framework
using the code first approach. 我正在学习使用代码优先方法的
Entity Framework
。 Take the example below, say I have a class Employee
: 以下面的示例为例,说我有一个
Employee
类:
public class employee
{
public int id{ get; set; }
public string fName { get; set; }
public string lName { get; set; }
public string email { get; set; }
}
It will automatically create the table using NVARCHAR(MAX)
for string
, and int
for int
. 它会自动创建一个使用表
NVARCHAR(MAX)
的string
,并int
为int
。
How do I control the data type
and data size
created in the database? 如何控制在数据库中创建的
data type
和data size
? (For example, I want to use CHAR(20) instead of just NVARCHAR(MAX)?) (例如,我想使用CHAR(20)而不是NVARCHAR(MAX)?)
Try the following: 请尝试以下操作:
public class TestContext : DbContext
{
public DbSet<employee> Employees { get; set; }
protected override void OnModelCreating(DbModelBuilder mb)
{
mb.Entity<employee>()
.Property(i => i.fName)
.HasColumnType("char")
.HasMaxLength(20);
base.OnModelCreating(mb);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.