zoukankan      html  css  js  c++  java
  • Entity Framework 6.0 Tutorials(3):Code-based Configuration

    Code-based Configuration:

    Entity Framework 6 has introduced code based configuration. Now, you can configure Entity Framework related settings using the code which had been previously configured in the <entityframework> section of the app.config. However, app.config takes precedence over code-based configuration. In other words, if a configuration option is set in both the code and in the config file, then the setting in the config file is used.

    Let's see how to implement code-based configuration using Entity Framework 6.

    First of all, you need to create a new class that derives the DbConfiguration (System.Data.Entity.DbConfiguration) class :

    public class FE6CodeConfig : DbConfiguration
    {
        public FE6CodeConfig()
        {
            //define configuration here
        }
    }

    Now, you can set codeConfigurationType in the config file as shown below:

    <entityFramework codeConfigurationType="EF6DBFirstTutorials.FE6CodeConfig, EF6DBFirstTutorials">
    </entityFramework>

    Or you can use the DbConfigurationType attribute on the context class to set the code-based configuration class:

    async query output

    Note: EF does not support having multiple configuration classes used in the same AppDomain. If you use this attribute, to set different configuration classes for two contexts, then an exception will be thrown.

    Now, you can use different methods of DbConfiguration using this in the constructor as shown below:

    async query output

    Let's see how to do different settings using code-based configuration as well as the config file.

    Set default connection factory:

    Code-based configuration:

    public class FE6CodeConfig : DbConfiguration
        {
            public FE6CodeConfig()
            {
                this.SetDefaultConnectionFactory(new System.Data.Entity.Infrastructure.SqlConnectionFactory());
            }
    }

    config file:

    <entityFramework>
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    </entityFramework>

    Set Database Provider:

    Code-based configuration:

    public class FE6CodeConfig : DbConfiguration
    {
        public FE6CodeConfig()
        {
        this.SetProviderServices("System.Data.SqlClient", 
                    System.Data.Entity.SqlServer.SqlProviderServices.Instance);
        }
    }

    config file:

    <entityFramework>
        <providers>
            <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
        </providers>
    </entityFramework>

    Set Database Initializers:

    You can set database initializers (for Code-First only) using code-based configuration as shown below:

    public class FE6CodeConfig : DbConfiguration
    {
            public FE6CodeConfig()
            {
                this.SetDatabaseInitializer<SchoolDBEntities>(new CustomDBInitializer<SchoolDBEntities>());
            }
    }

    config file:

    <entityFramework>
        <contexts>
            <context type="EF6DBFirstTutorials.SchoolDBEntities, EF6DBFirstTutorials"  >
            <databaseInitializer   type="EF6DBFirstTutorials.CustomDBInitializer , EF6DBFirstTutorials">
            </databaseInitializer>
            </context>
        </contexts>    
    </entityFramework>

    Download sample project for Code-based configuration demo.

  • 相关阅读:
    sencha touch 入门学习资料大全
    细说websocket
    【读fastclick源码有感】彻底解决tap“点透”,提升移动端点击响应速度
    新鲜的前端效果,边栏菜单、滑动效果
    PhoneGap+JQuery Mobile移动应用开发学习笔记
    21个值得收藏的Javascript技巧
    NodeJS无所不能:细数10个令人惊讶的NodeJS开源项目
    Node.js 中文学习资料和教程导航
    PayPal为什么从Java迁移到Node.js 性能提高一倍 文件代码减少44%
    知道创宇研发技能表v2.1
  • 原文地址:https://www.cnblogs.com/purplefox2008/p/5649525.html
Copyright © 2011-2022 走看看