• core 中ef 连接sql server数据库 在类库中 自动生成 model


    首先 介绍 

    Scaffold-DbContext "Server=.;database=sdd;User Id=sa;Password=123456;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir building    

    网上 很多生成 命名是下面这个:

    Scaffold-DbContext "Server=.;database=test1;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir building    
    生成的代码里面默认是这样的:

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
    if (!optionsBuilder.IsConfigured)
    {
    optionsBuilder.UseSqlServer(@"Server=localhost;database=oss1;Trusted_Connection=True;");////Trusted_Connection=True;为受信任连接,相当于windows身份验证模式。
    }

    }

    但当你发布之后 那就会 发现问题,发布之后无法访问 数据库,所以最好弄成 userid  password 的形式。

    开始 工作流程:

         一、安装Nuget包  如下图:

          

    第二步:将项目 切换到 类库下 (右击 类库)

    然后找到 程序包管理器控制台,输入指令:

    Scaffold-DbContext "Server=.;database=sdd;User Id=sa;Password=123456;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir building

    就会在 类库下 的 building 文件夹 下 生成 数据库 表对应model

    (如果 不切换项目,就会在 主项目下面 生成 building文件夹 和 model,不在类库下面)

    然后 在 主项目 的 startup里面 注册一下就好了

     services.AddDbContext<DateCore.building.oss1Context>();

     完工。

    参考文档链接:https://www.cnblogs.com/tianma3798/p/6835400.html

    今天重新做这类项目,发现以上会有问题,经处理已解决 -----2019/01/24

    解决方法:1.content 中删除无参数的构造函数,处理后这样:

     //public SavillsContext()
            //{
            //}
    
            public SavillsContext(DbContextOptions<SavillsContext> options)
                : base(options)
            {
            }
    

      2.现在没必要在contest中配置数据库连接了,可以注释掉:

     //protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            //{
            //    if (!optionsBuilder.IsConfigured)
            //    {
            //      //  var strda = AppConfigurtaionServices.Configuration.GetConnectionString("DefaultConnection");
            //        optionsBuilder.UseSqlServer("");
            //    }
            //}
    

      3.在start.cs中配置数据库路径:

     services.AddDbContext<Context>(options =>
                    
                    // 如果使用SQL Server 2008数据库,请添加UseRowNumberForPaging的选项
                   
                    options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"), b => b.UseRowNumberForPaging())
                    );
    

      

  • 相关阅读:
    Android之MessageQueue、Looper、Handler与消息循环
    Eclipse之相关快捷键
    Android之背景颜色小知识(笔记)
    Android之开发常用颜色
    Android之Handler与AsyncTask的区别
    Android之dip、dp、px、sp和屏幕密度
    the setting of serial port in the SecureCRT
    Raspberry Pi 3 Basic Command and Information
    Raspberry Pi 3 --- identify the version of linux kernal file
    Linux C/C++ ------ “” and <> in the use of head include file(Pending Verification)
  • 原文地址:https://www.cnblogs.com/fishyues/p/8931915.html
Copyright © 2020-2023  润新知