如果数据库不存在,自动创建数据库
This commit is contained in:
		
							parent
							
								
									dca499910f
								
							
						
					
					
						commit
						9bd424b540
					
				@ -1,32 +0,0 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using System.Runtime.CompilerServices;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
using Microsoft.AspNetCore.Builder;
 | 
			
		||||
using Microsoft.Extensions.DependencyInjection;
 | 
			
		||||
 | 
			
		||||
namespace StaffManagement.Database
 | 
			
		||||
{
 | 
			
		||||
    static public class DbInit
 | 
			
		||||
    {
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// 初始化数据库
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <param name="app"></param>
 | 
			
		||||
        /// <returns></returns>
 | 
			
		||||
        public static IApplicationBuilder SMDbInit(this IApplicationBuilder app) {
 | 
			
		||||
            app.ApplicationServices.GetService<SMDbContext>().Init();
 | 
			
		||||
            return app;
 | 
			
		||||
        }
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// 初始化数据库
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <param name="db">数据库上下文</param>
 | 
			
		||||
        /// <returns></returns>
 | 
			
		||||
        public static SMDbContext Init(this SMDbContext db) {
 | 
			
		||||
            db.Database.EnsureCreated();
 | 
			
		||||
            return db;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -66,7 +66,7 @@ namespace StaffManagement
 | 
			
		||||
                app.UseExceptionHandler("/Home/Error");
 | 
			
		||||
            }
 | 
			
		||||
            app.UseStaticFiles();
 | 
			
		||||
            db.Init();
 | 
			
		||||
            db.Database.EnsureCreated();
 | 
			
		||||
            app.UseSwagger();
 | 
			
		||||
            app.UseSwaggerUI(c => {
 | 
			
		||||
                c.SwaggerEndpoint("/swagger/V1/swagger.json","接口文档");
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user