Asp.Net Core 2.0 生成操作数据库文件需要的文件
2017-12-29 16:50
731 查看
开发环境
VS2017 + Win7 x64生成操作数据库文件需要配置或添加的文件
1、Startup.cs
public IConfiguration Configuration { get; } // 为了引入配置文件。默认的配置文件为"AppSettings.json"。 public Startup(IConfiguration configuration) { Configuration = configuration; } // This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { // 添加 Entity Framework 服务,并且使用SQL Server 服务。 services.AddEntityFrameworkSqlServer(). AddDbContext<WebDemoDbContext>(option => option.UseSqlServer(Configuration["database:connection"])); // 设置获取 appsettions.json 里的值 services.Configure<AppsettingOptions>(Configuration); services.AddMvc(); } // 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(); } // 调用wwwroot 里的文件 app.UseStaticFiles(); app.UseAuthentication(); // 默认首页 app.UseMvcWithDefaultRoute(); app.Run(async (context) => { await context.Response.WriteAsync("Hello World!"); }); }
2、Program.cs
public class Program { public static void Main(string[] args) { BuildWebHost(args).Run(); } public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() .UseStartup<Startup>() .UseApplicationInsights() .Build(); }
3、AppSettings.json
{ "database:connection": "Data Source=.;Initial Catalog=test;User ID=sa;Password=123" }
Models 文件夹中
4、model 类public class User { [Key] public int Id { get; set; } public string UserName{get;set;} }
5、DbContext 类
public class WebDemoDbContext: DbContext { // 用户信息 public virtual DbSet<User> User{ get; set; } public WebDemoDbContext(DbContextOptions<WebDemoDbContext> options) : base(options) { } }
6、编辑解决方案
添加如下代码:
<ItemGroup> <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" /> </ItemGroup>
相关文章推荐
- 【.NetCore学习】ASP.NET Core EF Core2.0 DB First现有数据库自动生成实体Context
- Asp.Net Core 2.0 项目实战(4)ADO.NET操作数据库封装、 EF Core操作及实例
- [译]ASP.NET Core 2.0 本地文件操作
- ASP.NET Core 2.0 本地文件操作问题及解决方案
- 在ASP.NET 2.0中,有时候需要对ASP.NET生成的HTML代码进行处理,或者是保存成静态文件。ASP.NET 提供了直接将请求保存成文件的方法:HttpRequest.SaveAs方法。下面这个方法就是在ASP.NET 2.0中得到ASP.NET
- log4net1.2.10 在asp.net 2.0当中的使用:写入文件或者数据库
- asp.net 2.0 页面中直接输出数据库中的txt、html文件格式流
- ASP.NET 2.0中执行数据库操作命令之二
- ASP.NET 2.0中执行数据库操作命令之一
- ASP.NET 2.0操作数据库
- asp.net当文件在不同目录下,需要获取数据库连接字符串
- asp.net 2.0 provider 数据库生成
- ASP.NET 2.0中执行数据库操作命令之一
- 【点滴】向Sql Express数据库文件中注册Asp.NET 2.0用户管理模块
- ASP.NET 2.0中执行数据库操作命令之二
- 用宏自动生成Web.sitemap文件(ASP.NET 2.0)
- 照葫芦画瓢,用ASP.NET 2.0在Oracle中存取图片(文件)的操作
- [导入]ASP.NET 2.0 遍历配置文件的数据库连接字符串
- ASP.NET 2.0 遍历配置文件的数据库连接字符串
- ASP.NET 2.0 - 如何把上传的文件保存到数据库字段 (转自章立民CnBlogs)