Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MVC4数据库未进行种子设定_C#_Asp.net Mvc 4_Visual Studio 2012_Model View Controller - Fatal编程技术网

C# MVC4数据库未进行种子设定

C# MVC4数据库未进行种子设定,c#,asp.net-mvc-4,visual-studio-2012,model-view-controller,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; nam

我首先使用代码为我的项目创建数据库,我在查看数据库时遇到了麻烦,我得到了一个空值错误。我假设这意味着种子数据不是种子数据,数据库是空的。 这是我的代码,任何洞察都会很棒:

型号:

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());}

    }
}
web.config中的con字符串

 <add name="AppCon" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-radCa1302;Integrated Security=SSPI;"
更新:发生错误的索引页,因此顶部的IEnumerable

 @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>
错误:
对象引用未设置为对象的实例。

引发空引用异常的位置在哪里?在索引页上,当我尝试启动该问题时,我将更新代码t显示此问题,忽略了它的tupid问题,但您是否在控制器中提取数据?是的,我有此代码来创建实例AppDB=new AppDB;然后我尝试使用///return Viewdb.Locations.ToList;要得到一个列表出来,但然后我仍然得到一个值不能为空的错误
 @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>