七、使用EF快速完成CRUD功能
(1)新建Artist,Genre,Album3个类,用属性显示3张表的关联
(2)添加DbContext类(对应数据库),添加DbSet<Model>(对应表)
>画出数据库设计图(表、表间关系),手动、UML、SQL的工具均可
>添加Album、Genre、Artist类,和DbContext类
public class DbMusicContext:DbContext //相当于数据库 { public DbSetAlbums { get; set; } //Album表 public DbSet Genres { get; set; } //Genre表 }
八、创建DCDIMC派生类,Database.SetInitializer(DCDIMC派生) 创建数据库并录入记录(DCDIMC的功能)
备注:>SampleData类必须继承DropCreateDatabaseIfModelChanges<T> 类,T是DbContext派生类(在此是DbMusicContext)
>DCDIMC类用于将已有数据添加到sdf中.若只关注数据的CRUD操作,八、九可以跳过.
(1)Webconfig中连接DbContext类。DataSource和provider Name
(2)添加DCDIMC 派生类,并override seed方法
namespace Music.Models{ public class SampleData : DropCreateDatabaseIfModelChanges{ protected override void Seed(DbContextMusic context) { var genres = new List { new Genre { Name = "Rock" }, new Genre { Name = "Jazz" }, new Genre { Name = "Metal" }, new Genre { Name = "Alternative" }, new Genre { Name = "Disco" }, new Genre { Name = "Blues" }, new Genre { Name = "Latin" }, new Genre { Name = "Reggae" }, new Genre { Name = "Pop" }, new Genre { Name = "Classical" } }; var artists = new List
(3)在Global.asax中 Database.SetInitilizer( new DCDIMC派生类)
protected void Application_Start() { Database.SetInitializer(new SampleData()); //SetInitializer的参数是 DCDIMC派生类 //new 数据库初始类名
运行效果:
创建了数据库MusicDb.sdf,并且添加了3张表。 在DCDIMC中声明的记录都已录入对应表.(记住,在DbContext中只声明了DbSet<Album>和DbSet<Genre>)
九、DCDIMC录入数据疑问
DCDIMC的Seed写法(1):
var genres = new List{ new Genre { Name = "Rock" }, .... new Genre { Name = "Classical" } }; var artists = new List { new Artist { Name = "Aaron Copland & London Symphony Orchestra" }, ......................... new Artist { Name = "Zeca Pagodinho" } }; var albums = new List { new Album { Title = "The Best Of Men At Work", Genre = genres.Single(g => g.Name == "Rock"), Price = 8.99M, Artist = artists.Single(a => a.Name == "Men At Work"), AlbumArtUrl = "/Content/Images/placeholder.gif" }, .... new Album { Title = "Ao Vivo [IMPORT]", Genre = genres.Single(g => g.Name == "Latin"), Price = 8.99M, Artist = artists.Single(a => a.Name == "Zeca Pagodinho"), AlbumArtUrl = "/Content/Images/placeholder.gif" } };
//执行后Album表、Genre表、Artist表都无任何记录
Seed写法2:
var genres = new List{ new Genre { Name = "Rock" }, .... new Genre { Name = "Classical" } }; var artists = new List { new Artist { Name = "Aaron Copland & London Symphony Orchestra" }, ......................... new Artist { Name = "Zeca Pagodinho" } }; new List { new Album { Title = "The Best Of Men At Work", Genre = genres.Single(g => g.Name == "Rock"), Price = 8.99M, Artist = artists.Single(a => a.Name == "Men At Work"), AlbumArtUrl = "/Content/Images/placeholder.gif" }, .... new Album { Title = "Ao Vivo [IMPORT]", Genre = genres.Single(g => g.Name == "Latin"), Price = 8.99M, Artist = artists.Single(a => a.Name == "Zeca Pagodinho"), AlbumArtUrl = "/Content/Images/placeholder.gif" } }.ForEach(a => context.Albums.Add(a));
//尽管只有context.Albums.add(a) ,记录都已经录入3个表.
十、实现Album的CRUD操作
(1)添加AlbumController
(2)向导中选择Model和DbContext //效果实现
(3)记忆Controller和Views中生成的代码
The following sections have been defined but have not been rendered for the layout paged的解决方式:
消除Edit.cshtml中的@setion渲染代码