zoukankan      html  css  js  c++  java
  • 【译】第21节---Fluent API

     原文:http://www.entityframeworktutorial.net/code-first/fluent-api-in-code-first.aspx

    在前面的学习中。我们已经看到不同的DataAnnotations属性来覆盖默认的Code-First约定。 本节,我们将了解Fluent API。

    Fluent API是配置你的域类的另一种方式。

    Fluent API为DataAnnotations提供了更多的配置功能。

    Fluent API支持以下类型的映射:

    让我们开始使用Fluent API。

    首先,我们创建Student和Standard域类和上下文类。

    现在,在上下文类中重写DBContext的OnModelCreating方法,如下所示:

    public class SchoolContext: DbContext 
    {
        public SchoolDBContext(): base() 
        {
        }
    
        public DbSet<Student> Students { get; set; }
        public DbSet<Standard> Standards { get; set; }
            
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            //Configure domain classes using modelBuilder here
    
            base.OnModelCreating(modelBuilder);
        }
    }

    现在,所有使用Fluent API的配置代码都应该使用OnModelCreating方法。 DbModelBuilder是一个主类,你可以在其中配置所有域类,因为此时所有域类都已初始化。

    您也可以同时使用DataAnnotation和Fluent API。 Code-First优先于Fluent API > 数据注解 > 默认约定。

    DbModelBuilder类包括重要的属性和配置方法。 有关DbModelBulder类的更多信息,请访问MSDN

    我们在下一节中使用Fluent API配置实体。

  • 相关阅读:
    S MVC 转发与重定向
    S MVC Controller方法返回值
    S MVC 表单提交
    numpy数据平滑
    Python入门
    Django
    python机器学习基础教程-监督学习
    drf-CBV
    numpy数组符号化与函数向量化
    numpy中多项式拟合与复数
  • 原文地址:https://www.cnblogs.com/talentzemin/p/7268168.html
Copyright © 2011-2022 走看看