Mvc Music Store(MVC3):SampleData不会填充DB

时间:2016-06-22 23:02:09

标签: c# asp.net asp.net-mvc asp.net-mvc-3

这是我真正的大问题:当我运行该程序时,它找不到任何类型):Check the picture

该示例使用此代码作为连接字符串:

 <connectionStrings>
    <add name="MusicStoreEntities"
    connectionString="Data Source=|DataDirectory|MvcMusicStore.sdf"
    providerName="System.Data.SqlServerCe.4.0"/>
 </connectionStrings>

您可能会注意到,它正在使用SqlServerCe4.0,但我改为使用SqlClient(我已安装):

<connectionStrings>
     <add 
            name="MusicStoreEntities"
             connectionString="Data Source=(localdb)\v11.0;
             Integrated Security=true;AttachDbFileName=|DataDirectory|MvcMusicStore.mdf"
             providerName="System.Data.SqlClient" 
      />
</connectionStrings>

示例数据文件:

using System.Collections.Generic;
using System.Linq;
using System.Data.Entity;

namespace MvcMusicStore.Models
{
    public class SampleData : DropCreateDatabaseIfModelChanges<MusicStoreEntities>
    {
        protected override void Seed(MusicStoreEntities context)
        {
            var genres = new List<Genre>
            {
                //Perfec genres
            };

            var artists = new List<Artist>
            {
                //Cool Artists list
            };

            new List<Album>
            {
                //Awesome albums
            }.ForEach(a => context.Albums.Add(a));

        }
    }
}

上下文类:

using System.Data.Entity;

namespace MvcMusicStore.Models
{
    public class MusicStoreEntities : DbContext
    {
        public DbSet<Album> Albums { get; set; }
        public DbSet<Genre> Genres { get; set; }
    }
}

应用程序启动类:

using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace MvcMusicStore
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            System.Data.Entity.Database.SetInitializer(new MvcMusicStore.Models.SampleData());

            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

        }
    }
}

如何解决这个问题?我会帮助你。

编辑1.添加了IdentityModels.cs

IdentityModels.cs

 public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {


    }

        public static ApplicationDbContext Create()
        {

            return new ApplicationDbContext();
        }
    }

2 个答案:

答案 0 :(得分:0)

尝试将上下文类更改为以下内容:

using System.Data.Entity;

namespace MvcMusicStore.Models
{
    public class MusicStoreEntities : DbContext
    {
        public MusicStoreEntities()
            : base("MusicStoreEntities")
        {

        }
        public DbSet<Album> Albums { get; set; }
        public DbSet<Genre> Genres { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Album>().ToTable("Album");
            base.OnModelCreating(modelBuilder);
            modelBuilder.Entity<Genre>().ToTable("Product");
            base.OnModelCreating(modelBuilder);
        }

        public static MusicStoreEntities Create()
        {
            return new MusicStoreEntities();
        }
    }
}

OnModelCreating在数据库中创建表。

答案 1 :(得分:0)

我将代码复制到了示例数据类中,并放入了在迁移过程中创建的配置文件中。然后,我在包管理器控制台中运行了**** enable-migration -force ****代码,然后运行了 update-database 。此外,我在方法末尾添加了 context.savechange()。并且我的数据库中装有上述数据。 enter image description here