c# - Entity Framework Code First - 确定数据类型

标签 c# entity-framework

我正在使用代码优先方法学习 Entity Framework 。拿下面的例子来说,假设我有一个类Employee:

public class employee
{
    public int id{ get; set; }
    public string fName { get; set; }
    public string lName { get; set; }
    public string email { get; set; }
}

它将自动使用 NVARCHAR(MAX)(对于 string)和 int(对于 int)创建表。

如何控制数据库中创建的数据类型数据大小? (例如,我想使用 CHAR(20) 而不仅仅是 NVARCHAR(MAX)?)

最佳答案

尝试以下操作:

    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);
        }
    }

关于c# - Entity Framework Code First - 确定数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26804466/

相关文章:

c# - Entity Framework 4.1 - TPT 预加载 - "The ResultType of the specified expression is not compatible with the required type"

.net - 处理 Entity Framework OptimisticConcurrencyException

c# - EF ObjectContext、服务和存储库 - 管理上下文生命周期。

C# Http 基本身份验证绕过

c# - 如何从数组创建不同值的字符串(包括其他字符)

c# - 如何使用带有 Razor View 的 Entity Framework (.edmx 模型)为 MVC4 或 MVC 5 创建局部 View ?

c# - 使用 gridview asp.net 进行分页时索引超出范围

c# - 正则表达式删除字符和提供的单词

c# - Entity Framework 代码首次迁移 : get sql scripts programmatically

c# - 将多个导航属性配置到同一个表时出错