zoukankan      html  css  js  c++  java
  • wpf中 efcore 两种配置方式

    1.外部传入

    public partial class App : Application
        {
            ServiceProvider serviceProvider { get; set; }
    
            protected override void OnStartup(StartupEventArgs e)
            {
                var service=new ServiceCollection();
    
                ConfigurationService(service);
    
                serviceProvider = service.BuildServiceProvider();
    
                var mainView = serviceProvider.GetRequiredService<MainWindow>();
                mainView.Show();
    
                base.OnStartup(e);
            }
    
            private void ConfigurationService(ServiceCollection service)
            {
                service.AddTransient(typeof(MainWindow));
                //service.AddDbContext<SchemeContext>();
    
                IConfiguration configuration;
                var builder = new ConfigurationBuilder();
    
                builder.AddJsonFile(System.Environment.CurrentDirectory+"\\AppSetting.json", false, true);
                configuration = builder.Build();
    
                var value=configuration.GetSection("Oracle");
                if(value == null)
                {
                    MessageBox.Show("请配置数据库连接");
                    Application.Current.Shutdown();
                }
    
    
                service.AddDbContext<SchemeContext>(option => { option.UseOracle(value.Value, f => f.UseOracleSQLCompatibility("12")); });
            }
        }
    
    1. 在数据库上下文中OnConfiguring方法中构造
    public class SchemeContext : DbContext
        {
    
            public SchemeContext(DbContextOptions options) : base(options)
            {
    
            }
    
            protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            {
                optionsBuilder.UseOracle("Data Source=(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.2.48)(PORT = 1521)))(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = CASICTEST)));User ID=mes;Password=mesadmin;", f => f.UseOracleSQLCompatibility("12"));
            }
    }
    
    留待后查,同时方便他人
    联系我:renhanlinbsl@163.com
  • 相关阅读:
    模糊匹配
    UPDATE SET FROM WHERE 续
    SQL SERVER 中row_number用法
    临时表和表变量
    镜像
    经典行列转换
    表记录查询最快查询方式
    NULL不是数值
    自增长值
    JSON
  • 原文地址:https://www.cnblogs.com/ives/p/15787307.html
Copyright © 2011-2022 走看看