• 【译】第43节---EF6-自定义约定


    原文:http://www.entityframeworktutorial.net/entityframework6/custom-conventions-codefirst.aspx

    Code-First对于有约定的模型有一组默认行为。 EF 6提供了自定义约定的能力,这将是模型的默认行为。

    有两种主要类型的约定,配置约定和模型约定。

    配置约定:

    配置约定是配置实体而不覆盖Fluent API中提供的默认行为的一种方式。 可以在OnModelCreating事件或自定义约定类中定义配置约定,类似于如何使用Fluent API定义正常的实体映射。

    例如,可以定义具有名称为{entity name} _ID的属性的实体的主键,例如。 学生实体的Student_ID属性将是主键:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder
            .Properties()
            .Where(p => p.Name == p.DeclaringType.Name + "_ID")
            .Configure(p => p.IsKey());
    
        base.OnModelCreating(modelBuilder);
    }

    下面的示例显示如何在所有实体中设置Description属性的字符串长度:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
            modelBuilder
                .Properties()
                .Where(p => p.Name == "Description")
                .Configure(p => p.HasMaxLength(200));
    
        base.OnModelCreating(modelBuilder);
    }

    还可以通过派生自Convention 类的自定义类来定义此约定,如下所示:

    public class PKConvention : Convention
    {
        public PKConvention()
        {
            .Properties()
            .Where(p => p.Name == p.DeclaringType.Name + "_ID")
            .Configure(p => p.IsKey());
    
        }
    }

    配置自定义约定如下所示:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Add<PKConvention>();
    }

    模型约定:

    模型约定基于底层模型元数据。 有CSDL和SSDL的惯例。 创建一个从CSDL约定实现IConceptualModelConvention的类,并为SSDL约定实现IStoreModelConvention。

    详细信息请移步:https://entityframework.codeplex.com/wikipage?title=Custom%20Conventions

  • 相关阅读:
    selenium server在页面加载超时浏览器与driver通信失败时的妙用
    selenium并行的使用
    selenium代理
    selenium鼠标拖动
    做事要是专业
    selenium select 标签选中
    从字符串中提取数字
    selenium中Alter等弹出对话框的处理
    Selenium资料
    P2048 [NOI2010]超级钢琴 (RMQ,堆)
  • 原文地址:https://www.cnblogs.com/talentzemin/p/7410736.html
Copyright © 2020-2023  润新知