MVC 4数据库不播种

时间:2015-03-15 14:17:40

标签: c# asp.net-mvc-4 visual-studio-2012 model-view-controller

我首先使用代码为我的项目创建数据库而且我遇到麻烦让我的数据库看到,我得到一个空值错误(我认为这意味着种子数据没有播种和数据库是空的)。 继承我的代码,任何见解都会很棒:

型号:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace rad_302_ca.Models
{
    public class SeederMethod : DropCreateDatabaseAlways<AppDB>
    {
        protected override void Seed(AppDB context)
        {
            var newlocations = new List<Location>()
            {
                new Location() { LocationName="Home",LocationAddress="Rathbrughan,Sligo"},
                new Location() {LocationName="Farm",LocationAddress="Mountcharles,Sligo"},
                new Location() {LocationName="College", LocationAddress="Sligo IT,Sligo"}
            };

            newlocations.ForEach(lo => context.Locations.Add(lo));
            context.SaveChanges();
        }
    }
    public class Location
    {
        [Key]
        public int LocationID { get; set; }
        public string LocationName { get; set; }
        public string LocationAddress { get; set; }
    }

    public class AppDB : DbContext
    {
        public DbSet<Location> Locations;
         public AppDB()
            : base("AppCon") { Database.SetInitializer(new SeederMethod());}

    }
}

全球:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using rad_302_ca.Models;
using System.Data.Entity;

namespace rad_302_ca
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class WebApiApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            Database.SetInitializer(new SeederMethod());
        }
    }
}

web.config中的con字符串

 <add name="AppCon" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-radCa1302;Integrated Security=SSPI;"

更新:发生错误的索引页面(也就是顶部的数字)

 @model IEnumerable<rad_302_ca.Models.Location>

<div class="row">
    <div class="col-md-2">
        <div class="panel panel-info">
            <div class="panel-heading">Locations</div>
            <div class="panel-body" style="overflow-x:hidden;height:300px">
                <table class="table table-condensed" id="loctbl">

                    @foreach (var item in Model)
                    {
                        <tr style="cursor:pointer"><td>@item.LocationID</td></tr>
                    }

                </table>
            </div>
        </div>
    </div>

错误: 对象引用未设置为对象的实例。

0 个答案:

没有答案