.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"
}
}
}
}
.net core 通过代码创建数据库表的更多相关文章
- hibernate动态创建数据库表名几种方式
数据库中数据量很大, 但又不可以删除时同时又要优化程序检索数据时间. 答:方式有很多比如 创建数据库表分区,创建索引, 存储过程等; 我这里采用动态创建数据库表的方式. 完全可以在不创建表分区情况下实 ...
- django使用model创建数据库表使用的字段
Django通过model层不可以创建数据库,但可以创建数据库表,以下是创建表的字段以及表字段的参数.一.字段1.models.AutoField 自增列= int(11) 如果没有的话,默认会生成一 ...
- Sql Server——运用代码创建数据库及约束
在没有学习运用代码创建数据库.表和约束之前,我们只能用鼠标点击操作,这样看起来就不那么直观(高大上)了. 在写代码前要知道在哪里写和怎么运行: 点击新建查询,然后中间的白色空白地方就是写代码的地方了. ...
- SQLAlchemy通过models创建数据库表
原地址:http://blog.csdn.net/jmilk/article/details/53184903 定义数据模型 models SQLAlchemy 允许我们根据数据库的表结构来创建数据模 ...
- SpringBoot使用Hibernate,实现自动创建数据库表【博客数据库设计】
我们准备设计博客,那就要设计数据库. 我们可以使用Hibernate来自动生成数据库. 博客数据库的结构: 实体类: 博客 Blog 博客分类 Type 博客标签 Tag 博客评论 Comment 用 ...
- 使用PowerDesigner创建数据库表图文并茂版
使用PowerDesigner创建数据库表图文并茂版 使用PowerDesigner 建数据库表. 一直很忙,没有时间写东西.这次搞点会声会色的,嘿嘿 此技能为项目经理必备技能. 本次主角: 1.在w ...
- 基于CentOS的MySQL学习补充三--使用Shell批量创建数据库表
本文出处:http://blog.csdn.net/u012377333/article/details/47006087 接上篇介绍<基于CentOS的Mysql学习补充二--使用Shell创 ...
- 数据库(一)--通过django创建数据库表并填充数据
django是不能创建数据库的,只能够创建数据库表,因此,我们在连接数据库的时候要先建立一个数据库. 在models.py中 from django.db import models class Pu ...
- Python 创建数据库表
创建数据库表 如果数据库连接存在我们可以使用execute()方法来为数据库创建表,如下所示创建表EMPLOYEE: #!/usr/bin/python # -*- coding: UTF-8 -*- ...
随机推荐
- 在npm上如何发布自己的包
环境需要:node和npm 然后在本地建一个文件夹作为你要发布的模块 然后在这个文件夹下打开cmd 执行:npm init 按照提示写对应的,或者先不写也可以,最后y 这时候在mcrm这个文件夹下就出 ...
- java 学习(day1)
之前学java没好好听课,会一点又不熟练,于是准备重新开始学一些细节,记录每日所学新知识. a+b java的a+b很有意思,当你输出的是" "+a+b,先假设a=2,b=3.然后 ...
- Jmeter-基础实战
一.测试需求:测试20个用户访问web网站在负载达到30QPS时的平均响应时间 QPS:Query Per Second 每秒查询率.是一台查询服务器每秒能够处理的查询次数.在因特网上,作为域名系统服 ...
- Python tip
shutil.rmtree() 表示递归删除文件夹下的所有子文件夹和子文件.
- 对malloc和free和数据结构和算法的一些感触
当年2013.9.大一学c程序设计,因为当时还没有学数据结构,只学了程序设计,大学上的课真的是承上启下的不好,刚学到这里,就断了旋一样,对这个malloc和free一直很迷惑,这些狗玩意是干嘛,因为用 ...
- C# ODP.Net oracle数据库操作 支持不安装客户端
下载: http://download.oracle.com/otn/other/ole-oo4o/ODTwithODAC1110720.zip?AuthParam=1414811820_e61f2f ...
- 以POST方式发送
URL url = null; String inputLine = null; HttpURLConnection httpurlconnection = null; try { //取上级电警平台 ...
- (转)数据索引BTree
.B-tree 转自:http://blog.csdn.net/hbhhww/article/details/8206846 B-tree又叫平衡多路查找树.一棵m阶的B-tree (m叉树)的特性如 ...
- (转)HDFS简介
转自:http://os.51cto.com/art/201212/369564.html
- Form表单利用Jquery Validate验证以及ajax提交
#表单利用Jquery验证验证以及ajax提交 概述>详细讲解表单利用Jquery >验证验证以及ajax提交的过程,以及Validate的自定义提示语,非空判断,输入字段的远程ajax验 ...