• Aspnetcore2.0中Entityframeworkcore及Autofac的使用(二)(附Demo)(


    一,通过Entityframeworkcore中DbFirst模式创建模型

    这里只说一下Entityframeworkcore中DbFirst模式创建模型,想了解CodeFirst的可以自行度娘,还是在原有项目中创建一个Model类库,然后通过通过vs中NuGet的程序包管理控制台执行

     

    安装EntityFrameworkCore类工具包,:

    Microsoft.EntityFrameworkCore.SqlServer

    Microsoft.EntityFrameworkCore.SqlServer.Design

    Microsoft.EntityFrameworkCore.Tools

    注意安装以下类库版本需与AspNetCore.Model自建类库的Core版本相适应。

    通过执行以下命令生成实体类与上下文

     Scaffold-dbcontext "Server=.;database=test1;Integrated Security=false;user id=****;password=*****" Microsoft.EntityFrameworkCore.SqlServer -outputdir Models

     

    生成成功后要修改一下test1Context.cs中的代码

     //protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            //{
            //    if (!optionsBuilder.IsConfigured)
            //    {
            //        optionsBuilder.UseSqlServer(@"Server=zhang;database=test1;Integrated Security=false;user id=sa;password=123456");
            //    }
            //}
    
            //自定义定义构造器
            public test1Context(DbContextOptions options):base(options)
            {
    
            }

    注意:每次更新数据库都要修改test1Context.cs中的代码

    新建BookService.cs

    namespace AspNetCore.Service
    {
        public class BookService
        {
            private test1Context _context = null;
    
            public BookService(test1Context context)
            {
                this._context = context;
            }
    
    
            public Book Get(long id)
            {
                return _context.Book.Find(id);
            }
        }
    }
    View Code

    下一步我们就要通过Controller把数据呈现出来,在这里我们要把数据库连接放到appsettings.json配置文件里

    {
        "ConnectionStrings": {
            "connstr": "Data Source=.;Initial Catalog=test1;User ID=*****; Password=**********"
        },
        "Logging": {
            "IncludeScopes": false,
            "LogLevel": {
                "Default": "Warning"
            }
        }
    }
    View Code

    修改一下项目启动文件Startup.cs:

     public void ConfigureServices(IServiceCollection services)
            {
                services.AddMvc();
                //配置上下文
                services.AddDbContext<test1Context>(options => options.UseSqlServer(Configuration.GetConnectionString("connstr")));
                //默认类库声明周期
                services.AddScoped(typeof(BookService));
            }
    View Code

    新建BookController中的代码:

    public class BookController : Controller
        {
            //依赖注入
            private readonly BookService _service = null;
            public BookController(BookService service)
            {
                this._service = service;
            }
    
            public IActionResult Index()
            {
                return Content($"第一本书的书名是[{_service.Get(1).Name}]") ;
            }
        }
    View Code

    结果:

     ^v^是不是很开森

  • 相关阅读:
    数据结构学习(十二)、归并排序
    数据结构学习(十一)、堆排序
    数据结构学习(十)、插入排序
    数据结构学习(九)、简单选择排序
    数据结构学习(八)、冒泡排序
    天梯赛 L3-013 非常弹的球 找规律
    codeup模拟赛 进击的二叉查找数
    POJ 2828 Buy Tickets 线段树 倒序插入 节点空位预留(思路巧妙)
    HDU 1394 逆序数 线段树单点跟新 | 暴力
    HDU 2795 Billboard 线段树,区间最大值,单点更新
  • 原文地址:https://www.cnblogs.com/MrLiu90/p/10062468.html
Copyright © 2020-2023  润新知