MvcMusicStore数据库连接 [英] MvcMusicStore database connection
问题描述
我下面的asp.net MvcMusicStore教程,一切工作正常,但数据不加入到MvcMusicStore.sdf ......这是使连接到数据库教程的那部分页面:A HREF <= http://www.asp.net/mvc/overview/older-versions/mvc-music-store/mvc-music-store-part-4 rel=\"nofollow\">http://www.asp.net/mvc/overview/older-versions/mvc-music-store/mvc-music-store-part-4
预期的结果应该返回一个列表与所有类型的,但对我来说,我在MvcMusicStore.sdf流派表中获取NULL值。
我下载了完整的教程,一切工作正常出现,所有的数据值插入到数据库表。
SampleData.cs:
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用System.Data.Entity的;命名空间MvcMusicStore.Models
{
公共类的sampleData:DropCreateDatabaseIfModelChanges&LT; MusicStoreEntities&GT;
{
保护覆盖无效的种子(MusicStoreEntities上下文)
{
VAR流派=新的List&LT;&体裁GT;
{
新的流派名称{=岩石},
新的流派名称{=爵士},
新的流派名称{=金属}
}; VAR艺术家=新的List&LT;艺术家&GT;
{
新人{名称=科普兰和放大器;伦敦交响乐团},
新人{名称=亚伦·戈德堡}
}; 新的List&LT;相册和GT;
{
新专辑{标题=A科普兰庆典,第一卷,类型= genres.Single(G =&GT; g.Name ==古典),价钱= 8.99M,艺术家= artists.Single(A =&GT; a.Name ==科普兰和放大器;伦敦交响乐团),AlbumArtUrl =/Content/Images/placeholder.gif},
} .ForEach(一个= GT; context.Albums.Add(一));
}
}
}
Web.config文件:
&LT;&是connectionStrings GT;
&LT;添加名称=MusicStoreEntities
的connectionString =数据源= | DataDirectory目录| MvcMusicStore.sdf
的providerName =System.Data.SqlServerCe.4.0/&GT;
&LT; /&是connectionStrings GT;
Global.asax中
保护无效的Application_Start()
{
System.Data.Entity.Database.SetInitializer(
新MvcMusicStore.Models.SampleData());
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
的RegisterRoutes(RouteTable.Routes);
}
Genre.cs
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;命名空间MvcMusicStore.Models
{
公共部分类体裁
{
公众诠释GenreId {搞定;组; }
公共字符串名称{;组; }
公共字符串描述{搞定;组; }
公开名单&LT;相册和GT;相册{搞定;组; }
}
}
您不能有效地加入流派和艺术家到数据库中。
行 context.Albums.Add(一)
添加专辑到上下文但流派和艺术家的等效调用失踪。
以下各行应添加:
-
context.Artists.AddRange(艺术家)
-
context.Genres.AddRange(流派)
I am following the asp.net MvcMusicStore tutorial and everything works fine but the data is not adding to MvcMusicStore.sdf...This is the page with that part of the tutorial that makes the connection to database: http://www.asp.net/mvc/overview/older-versions/mvc-music-store/mvc-music-store-part-4 The expected result should return a list with all of genres, but in my case I'm getting NULL values in MvcMusicStore.sdf Genres table.
I downloaded the completed tutorial and everything works fine there, all data values are inserted to database tables..
SampleData.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace MvcMusicStore.Models
{
public class SampleData : DropCreateDatabaseIfModelChanges<MusicStoreEntities>
{
protected override void Seed(MusicStoreEntities context)
{
var genres = new List<Genre>
{
new Genre { Name = "Rock" },
new Genre { Name = "Jazz" },
new Genre { Name = "Metal" }
};
var artists = new List<Artist>
{
new Artist { Name = "Aaron Copland & London Symphony Orchestra" },
new Artist { Name = "Aaron Goldberg" }
};
new List<Album>
{
new Album { Title = "A Copland Celebration, Vol. I", Genre = genres.Single(g => g.Name == "Classical"), Price = 8.99M, Artist = artists.Single(a => a.Name == "Aaron Copland & London Symphony Orchestra"), AlbumArtUrl = "/Content/Images/placeholder.gif" },
}.ForEach(a => context.Albums.Add(a));
}
}
}
Web.config:
<connectionStrings>
<add name="MusicStoreEntities"
connectionString="Data Source=|DataDirectory|MvcMusicStore.sdf"
providerName="System.Data.SqlServerCe.4.0"/>
</connectionStrings>
Global.asax
protected void Application_Start()
{
System.Data.Entity.Database.SetInitializer(
new MvcMusicStore.Models.SampleData());
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
Genre.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcMusicStore.Models
{
public partial class Genre
{
public int GenreId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public List<Album> Albums { get; set; }
}
}
You are not effectively adding the genres and artists to the database.
The line context.Albums.Add(a)
adds the albums to the context but the equivalent calls for genres and artists are missing.
The following lines should be added:
context.Artists.AddRange(artists)
context.Genres.AddRange(genres)
这篇关于MvcMusicStore数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!