zoukankan      html  css  js  c++  java
  • .net core 通过代码创建数据库表

    0.结构: 

    1.API

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.Logging;
    
    namespace LJS.Api
    {
        public class Program
        {
            public static void Main(string[] args)
            {
                CreateWebHostBuilder(args).Build().Run();
            }
    
            public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
                WebHost.CreateDefaultBuilder(args).UseIIS().UseStartup<Startup>();
        }
    }
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Threading.Tasks;
    using LJS.AppSrv;
    using LJS.Domains;
    using LJS.Utils;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.AspNetCore.HttpsPolicy;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Logging;
    using Microsoft.Extensions.Options;
    using Newtonsoft.Json.Serialization;
    using Swashbuckle.AspNetCore.Swagger;
    
    namespace LJS.Api
    {
        public class Startup
        {
            public Startup(IConfiguration configuration)
            {
                Configuration = configuration;
                AppSetting.SetAppSetting(Configuration.GetSection("ConfigurationInfo"));
            }
    
            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.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
                services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2).AddJsonOptions(options => { options.SerializerSettings.ContractResolver = new DefaultContractResolver(); }); ;
                //  services.AddMvcCore().AddApiExplorer();
                services.AddSwaggerGen(c =>
                {
                    c.SwaggerDoc("v1", new Info { Title = "信息API", Version = "v1", Contact = new Contact() { Name = "作者", Email = "电子邮箱" } });
    
                    var basepath = System.AppContext.BaseDirectory;
                    string[] arr = new string[] { "LJS.AppSrv.xml", "LJS.Api.xml" };
    
                    foreach (var item in arr)
                    {
                        var xmlpath = Path.Combine(basepath, item);
                        c.IncludeXmlComments(xmlpath);
                    }
                });
    
                string[] urls = Configuration.GetSection("AllowedCors").Value.Split(",");
                services.AddCors(c => c.AddPolicy("AllowAllOrigin", bulid =>
                {
                    bulid.WithOrigins(urls).AllowAnyMethod().AllowAnyHeader().AllowCredentials();
                }));
            }
    
            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app, IHostingEnvironment env)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
                else
                {
                    app.UseHsts();
                }
    
                app.UseHttpsRedirection();
    
                // Enable middleware to serve generated Swagger as a JSON endpoint.
    
    
                app.UseCors("AllowAllOrigin");
    
                // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), 
                // specifying the Swagger JSON endpoint.
                app.UseSwagger();
    
                app.UseSwaggerUI(c =>
                {
                    c.SwaggerEndpoint("/swagger/v1/swagger.json", "API V1");
                    //c.ShowRequestHeaders();
                });
                //app.UseMvc(routes =>
                //{
                //    routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
                //});
                app.UseMvc();
            }
        }
    }

    2.设置API为启动项目

    3.在VS打开 程序包管理控制台,选择DOMAINS项目

     

    4.运行

    Add-Migration "PartXXMigs" -OutputDir "Migrations" -Context "DbXXContext"

    Update-Database "PartXXMigs" -Context "DbXXContext"

    5.api - launchSettings.json

    {
      "$schema": "http://json.schemastore.org/launchsettings.json",
      "iisSettings": {
        "windowsAuthentication": false,
        "anonymousAuthentication": true,
        "iisExpress": {
          "applicationUrl": "http://localhost:62806",
          "sslPort": 0
        }
      },
      "profiles": {
        "IIS Express": {
          "commandName": "IISExpress",
          "launchBrowser": true,
          "launchUrl": "swagger",
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
          }
        },
        "LJS.Api": {
          "commandName": "Project",
          "launchBrowser": true,
          "launchUrl": "swagger",
          "applicationUrl": "http://localhost:5000",
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
          }
        }
      }
    }
  • 相关阅读:
    c#中使用log4net工具记录日志
    由命名空间函数而引发思考--js中的对象赋值问题
    浅析C#中的Attribute[转]
    剑指offer_输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果
    剑指offer_输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径
    剑指offer_输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字
    HashSet TreeSet 中元素顺序问题(未解决)
    找出某个String中出现次数最多的字符,并输出次数(字符较长)
    MyBatis、JDBC、Hibernate区别
    String.equals用法注意
  • 原文地址:https://www.cnblogs.com/jasonlai2016/p/11069097.html
Copyright © 2011-2022 走看看