博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MVC Music Sotre -2
阅读量:6669 次
发布时间:2019-06-25

本文共 4027 字,大约阅读时间需要 13 分钟。

 

七、使用EF快速完成CRUD功能

(1)新建Artist,Genre,Album3个类,用属性显示3张表的关联

(2)添加DbContext类(对应数据库),添加DbSet<Model>(对应表) 

 >画出数据库设计图(表、表间关系),手动、UML、SQL的工具均可

  

>添加Album、Genre、Artist类,和DbContext类

public class DbMusicContext:DbContext //相当于数据库    {        public DbSet
Albums { 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渲染代码

    

转载地址:http://vcoxo.baihongyu.com/

你可能感兴趣的文章
Spring Security HTTP Basic for RESTFul and FormLogin (Cookies) for web - Annotations
查看>>
mysql 5.5.32 多实例环境的启动问题
查看>>
js或css文件后面的参数是什么意思?
查看>>
2016 年度开源中国新增开源软件排行榜 TOP 100
查看>>
nginx实时生成缩略图到硬盘上
查看>>
一个远程启动windows c++程序引发的技术决策现象
查看>>
SQL查询与修改数据库逻辑文件名,移动数据库存储路径示例
查看>>
WebRTC 学习之 WebRTC 简介
查看>>
海量数据求中位数
查看>>
Python 黑帽编程 4.2 Sniffer之数据本地存储和加载
查看>>
跟锦数学2016年
查看>>
7 云计算系列之Neutron安装与配置
查看>>
编译器的主要组成部分
查看>>
转载:Kafka 之 中级 原作者:悟性
查看>>
【Tomcat】Tomcat闪退的问题解决/Tomcat修改端口号无效
查看>>
HTML5简单入门系列(一)
查看>>
RxJava
查看>>
Android 安卓真机调试 出现Installation error: INSTALL_FAILED_UPDATE_INCOMPATIBLE....
查看>>
Redis和Memcache对比及选择
查看>>
MonoBehaviour.print和Debug.Log是同样的作用
查看>>