【发布时间】:2015-08-28 09:38:44
【问题描述】:
我正在关注 asp.net MvcMusicStore 教程,一切正常,但数据未添加到 MvcMusicStore.sdf...这是教程中用于连接数据库的页面:http://www.asp.net/mvc/overview/older-versions/mvc-music-store/mvc-music-store-part-4 预期的结果应该返回一个包含所有流派的列表,但在我的情况下,我在 MvcMusicStore.sdf Genres 表中得到了 NULL 值。
我下载了完整的教程,那里一切正常,所有数据值都插入到数据库表中..
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>
全球.asax
protected void Application_Start()
{
System.Data.Entity.Database.SetInitializer(
new MvcMusicStore.Models.SampleData());
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
流派.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; }
}
}
【问题讨论】:
-
你能把你的流派模型贴出来吗?
-
DataDirectory你替换了这个吗? -
@MatthiasBurger 我已经用流派类更新了代码
标签: c# asp.net asp.net-mvc-3