009.Working with SQL Server LocalDB –【在sql server localdb 上操作数据】

  • From the View menu, open SQL Server Object Explorer (SSOX).

The app shows the seeded data.

Working with SQL Server LocalDB

Test the app

图片 1

图片 2

运用显示出开始化后的数目: 

若果数据库中有数量记录,就会一贯再次回到,如若没有就会增加那一个开头数据。

若是您运行vs在非调试方式,按F5运转进入调试方式

 1             app.UseStaticFiles();
 2 
 3             app.UseMvc(routes =>
 4 
 5             {
 6 
 7                 routes.MapRoute(
 8 
 9                     name: "default",
10 
11                     template: "{controller=Home}/{action=Index}/{id?}");
12 
13             });
14 
15  
16 
17             SeedData.Initialize(app.ApplicationServices);
18 
19         }
20 
21     }
22 
23 }

图片 3图片 4

默许意况下,LocalDB
会在C:/Users/<user> 文件夹下创立”*.mdf” 数据库文件。

图片 5图片 6

C# Code

Seed the database

Add the seed initializer to the end of the Configure method in
the Startup.cs file:

Asp.net core
的配置种类读取了ConnectionString 的配备的值。

View  菜单,打开 SQL
Server Object Explorer

  • Delete all the records in the DB. You can do this with the delete
    links in the browser or from SSOX.
1 if (context.Movie.Any())
2 
3 {
4 
5     return;   // DB has been seeded.
6 
7 }

强制行使起先化,在 Startup 
类中seed 方法会被实施。

LocalDB is a lightweight version of the SQL Server Express Database
Engine that is targeted for program development.

SQL Server Express
LocalDB
 
sql server 的一个简化免费版本

向数据库开头化值

  1 using Microsoft.EntityFrameworkCore;
  2 
  3 using Microsoft.Extensions.DependencyInjection;
  4 
  5 using System;
  6 
  7 using System.Linq;
  8 
  9  
 10 
 11 namespace MvcMovie.Models
 12 
 13 {
 14 
 15     public static class SeedData
 16 
 17     {
 18 
 19         public static void Initialize(IServiceProvider serviceProvider)
 20 
 21         {
 22 
 23             using (var context = new MvcMovieContext(
 24 
 25                 serviceProvider.GetRequiredService<DbContextOptions<MvcMovieContext>>()))
 26 
 27             {
 28 
 29                 // Look for any movies.
 30 
 31                 if (context.Movie.Any())
 32 
 33                 {
 34 
 35                     return;   // DB has been seeded
 36 
 37                 }
 38 
 39  
 40 
 41                 context.Movie.AddRange(
 42 
 43                      new Movie
 44 
 45                      {
 46 
 47                          Title = "When Harry Met Sally",
 48 
 49                          ReleaseDate = DateTime.Parse("1989-1-11"),
 50 
 51                          Genre = "Romantic Comedy",
 52 
 53                          Price = 7.99M
 54 
 55                      },
 56 
 57  
 58 
 59                      new Movie
 60 
 61                      {
 62 
 63                          Title = "Ghostbusters ",
 64 
 65                          ReleaseDate = DateTime.Parse("1984-3-13"),
 66 
 67                          Genre = "Comedy",
 68 
 69                          Price = 8.99M
 70 
 71                      },
 72 
 73  
 74 
 75                      new Movie
 76 
 77                      {
 78 
 79                          Title = "Ghostbusters 2",
 80 
 81                          ReleaseDate = DateTime.Parse("1986-2-23"),
 82 
 83                          Genre = "Comedy",
 84 
 85                          Price = 9.99M
 86 
 87                      },
 88 
 89  
 90 
 91                    new Movie
 92 
 93                    {
 94 
 95                        Title = "Rio Bravo",
 96 
 97                        ReleaseDate = DateTime.Parse("1959-4-15"),
 98 
 99                        Genre = "Western",
100 
101                        Price = 3.99M
102 
103                    }
104 
105                 );
106 
107                 context.SaveChanges();
108 
109             }
110 
111         }
112 
113     }
114 
115 }

 

图片 7

  • Right click on the Movie table > View Data

图片 8图片 9

图片 10图片 11

右击 Movie  表,点击  >
View Designer
菜单

LocalDB 是 SQL Server Express
Database Engine 的一个轻量版本,目标是本土的次第支付。

Create a new class named SeedData in the Models folder. Replace the
generated code with the following:

图片 12

测试应用

2017-3-7 2 分钟阅读时长 

JSON Code

  • Force the app to initialize (call the methods in the Startup class)
    so the seed method runs.

If there are any movies in the DB, the seed initializer returns and no
movies are added.

When you deploy the app to a test or production server, you can use an
environment variable or another approach to set the connection string to
a real SQL Server.

右击 Movie 表,选择 > View
Data
菜单

正文内容

SQL Server Express LocalDB

(一个简化的免费的轻量的sql
server 版本)

  • If you were running VS in non-debug mode, press F5 to run in debug
    mode

图片 13

 

右击系统托盘上通报区域的iis图标,并点击
Exit or Stop Site

当你将运用安顿到一个测试或生产服务器,你可以运用环境变量或此外的法子来设置一个实打实的db链接字符串的值。

1.SQL Server Express
LocalDB

MvcMovieContext
对象处理了链接数据库与映射Movie 对象到表记录的职责.

The MvcMovieContext object handles the task of connecting to the
database and mapping Movie objects to database records.

2.Seed the
database

 

 

By default, LocalDB database creates “*.mdf” files in
the C:/Users/<user> directory.

开头化数据库的开首表数据

  • Right click the IIS Express system tray icon in the notification
    area and tap Exit or Stop Site

                                    2017-08-14
15:22 周一

The database context is registered with the Dependency
Injection
 container
in the ConfigureServices method in the Startup.cs file:

图片 14

The ASP.NET
Core Configuration system
reads the ConnectionString.

C# Code

                                         蒙

如若你运行vs在调试情势,为止调试并按下F5

LocalDB
以用户形式直接起先询问即可,因而未曾复杂的安排。

图片 15

要刨除db中的所有数据记录,你可以在
SSOX 中点击删除链接即可。

为了强制伊始化,iis express
必须重启一下。你可以应用以下办法的任一种来形成:

1 "ConnectionStrings": {
2 
3   "MvcMovieContext": "Server=(localdb)\\mssqllocaldb;Database=MvcMovieContext-20613a4b-deb5-4145-b6cc-a5fd19afda13;Trusted_Connection=True;MultipleActiveResultSets=true"
4 
5 }

To force initialization, IIS Express must be stopped and restarted. You
can do this with any of the following approaches:

For local development, it gets the connection string from
the appsettings.json file:

图片 16

对此本地开发,它从appsettings.json
文件获取链接字符串的值:

C# code

图片 17图片 18

Models
文件夹下新建一个名为SeedData
类,用下边的代码替换掉自动生成的代码:

查看Configuration
获取越多新闻。

  • Right click on the Movie table > View Designer

在sql server localdb
上操作数据

See Configuration for
more information.

 1 public void ConfigureServices(IServiceCollection services)
 2 
 3 {
 4 
 5     // Add framework services.
 6 
 7     services.AddMvc();
 8 
 9  
10 
11     services.AddDbContext<MvcMovieContext>(options =>
12 
13             options.UseSqlServer(Configuration.GetConnectionString("MvcMovieContext")));
14 
15 }

把数据开始化类添加到Startup.cs
文件的Configure  方法的末尾一行:

 

C# Code

DB上下文在Startup.cs
文件的ConfigureServices 方法中被登记到了DI容器中:

By Rick Anderson

  • If you were running VS in debug mode, stop the debugger and press F5

Note the key icon next to ID. By default, EF will make a property
named ID the primary key.

只顾PK图标在ID
字段旁边,EF默许会使用ID做为一个PK。

LocalDB starts on demand and runs in user mode, so there is no complex
configuration.

相关文章