如何使用实体框架数据库第一种方法指定表模式 [英] How to specify Table schema using entity framework databse first approach

查看:55
本文介绍了如何使用实体框架数据库第一种方法指定表模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用实体框架数据库第一种方法指定表模式

How to specify Table schema using entity framework databse first approach

推荐答案

您应该根据您的模型创建配置类。在此您可以指定行为该属性

You should create a configuration class based on your model.In this you can specify the behavior of that property
public class SalesOrderConfiguration:EntityTypeConfiguration<salesorder>
    {
       public SalesOrderConfiguration()
       {
           Property(co => co.CustomerName).HasMaxLength(30).IsRequired();
           Property(co => co.PONumber).HasMaxLength(10).IsOptional();
       }
    }





您的模型类如下所示



Your model class will look like below

public class SalesOrder
   {

       public int SalesOrderId { get; set; }
       public string CustomerName { get; set; }
       public string PONumber { get; set; }
   }





您可以在DbContext中指定配置类



You can specify the configuration class in your DbContext

public class SalesContext:DbContext
   {
       public SalesContext()
           : base("DefaultConnection")
       {

       }

       public DbSet<salesorder> SalesOrders { get; set; }

       protected override void OnModelCreating(DbModelBuilder modelBuilder)
       {
           modelBuilder.Configurations.Add(new SalesOrderConfiguration());
       }
   }</salesorder>





希望这有帮助



Hope this helps


这篇关于如何使用实体框架数据库第一种方法指定表模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆