• 基于EFCore的CodeFirst实战(一)总体介绍


    本节目标

    1 创建实体类Entity

    2 创建数据库操作上下文DbContext

    3 数据库迁移

    实战内容

    基于AspNetCore3.1

    数据库:Sqlserver2014

    开发工具:Vs2019

    解决方案:gitee上Xwy.WebApiDemo

    项目:Xwy.Domain

    依赖库:Microsoft.EntityFrameworkCore,Microsoft.EntityFrameworkCore.SqlServerMicrosoft.EntityFrameworkCore.Tools,Microsoft.EntityFrameworkCore.Design

    代码

     项目结构

    依赖项都要安装,两个项目都要

    Install-Package Microsoft.EntityFrameworkCore
    Install-Package Microsoft.EntityFrameworkCore.SqlServer
    Install-Package Microsoft.EntityFrameworkCore.Tools
    Install-Package Microsoft.EntityFrameworkCore.Design
    一个数据库上下文对象时
    add-migration init
    update-database

    多个数据库上下文对象时
    add-migration init -Context VueShopDbContext update-database -Context VueShopDbContext

    数据库上下文类

    using Microsoft.EntityFrameworkCore;
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Xwy.Domain.Entities;
    
    namespace Xwy.Domain.DbContexts
    {
        public class VueShopDbContext:DbContext
        {
            public VueShopDbContext()
            {
    
            }
            public VueShopDbContext(DbContextOptions<VueShopDbContext> options):base(options)
            {
    
            }
            // 放入可以使用的数据库对象
            public DbSet<TestRecord> TestRecords { get; set; }
            protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            {
                if (!optionsBuilder.IsConfigured)
                {
                    optionsBuilder.UseSqlServer("Data Source=.;database=VueShopDb;uid=sa;pwd=123456");
                    //optionsBuilder.UseSqlServer("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=TestDB;Data Source=.");//数据库连接字符串,其中TestDB是数据库名称
                }
            }
            protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                modelBuilder.Entity<TestRecord>().ToTable("TestRecords","xwy");
                base.OnModelCreating(modelBuilder);
            }
        }
    }

    实体类

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace Xwy.Domain.Entities
    {
        public class TestRecord
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string Title { get; set; }
    
        }
    }

    启动项目配置代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Hosting;
    using Microsoft.Extensions.Logging;
    using Microsoft.OpenApi.Models;
    using Xwy.Domain.DbContexts;
    
    namespace Xwy.WebApiDemo
    {
        public class Startup
        {
            public Startup(IConfiguration configuration)
            {
                Configuration = configuration;
            }
    
            public IConfiguration Configuration { get; }
    
            // This method gets called by the runtime. Use this method to add services to the container.
            public void ConfigureServices(IServiceCollection services)
            {
                services.AddDbContext<ClothersMallDbContext>(m=> { 
                    
                });
                services.AddSwaggerGen(m => 
                {
                    m.SwaggerDoc("v1",new OpenApiInfo() { Title="swaggertest",Version="v1"});
                });
                services.AddCors(m => m.AddPolicy("any", a => a.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader()));
                services.AddControllers();
            }
    
            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
    
                app.UseRouting();
    
                app.UseAuthorization();
    
                app.UseSwagger();
                app.UseCors();
                app.UseSwaggerUI(m=> {
                    m.SwaggerEndpoint("/swagger/v1/swagger.json","swaggertest");
    
                });
    
                app.UseEndpoints(endpoints =>
                {
                    endpoints.MapControllers();
                });
            }
        }
    }
  • 相关阅读:
    文件上传,跨浏览器统一的样式
    JAVA与JSON的序列化、反序列化
    错误记录--更改tomcat端口号方法,Several ports (8005, 8080, 8009)
    45个非常有用的 Oracle 查询语句小结
    三分钟学会不吃球
    Linux命令:TOP
    【Oracle】Oracle官方文档
    【MySQL】MySQL官方文档
    【oracle】处理oracle用户密码中的特殊字符$和@
    【shell】整数运算,小数运算
  • 原文地址:https://www.cnblogs.com/xiewenyu/p/13123719.html
Copyright © 2020-2023  润新知