• 基于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();
                });
            }
        }
    }
  • 相关阅读:
    如何把pdf文档转化为word
    Win7系统中wmiprvse.exe占用CPU高如何解决
    Visual studio加载项目时报错 尚未配置为Web项目XXXX指定的本地IIS,需要配置虚拟目录。解决办法。
    SQL Server新增用户并控制访问权限设置。
    vs2013 中已经添加了引用,编译还是提示没有添加引用
    vue-cli中引入jquery方法
    vue-vuex安装
    vue2.0项目结构和打包发布
    从(0)新开始vue2.0【安装】
    js判断网络连接情况:navigator.onLine
  • 原文地址:https://www.cnblogs.com/xiewenyu/p/13123719.html
Copyright © 2020-2023  润新知