0

我需要帮助先生,MVC的新手,我想问一下为什么我找不到商店数据库,即使它在底部声明。

当前上下文中不存在“storeDB”

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using MyMusicStore.Models;

    namespace MyMusicStore.Controllers
    {
        public class StoreController : Controller
        {
            //
            // GET: /Store/

            public ActionResult Index()
            {
                var genres = storeDB.Genres.ToList();
                return View(genres);
            }

            public ActionResult Browse(string genre)
            {
                var newGenre = new Genre { Name = genre };
                return View (newGenre);
            }

            public ActionResult Details(int id)
            {
                var album = new Album { Title = "Album" + id };
                return View(album);
            }

            public class StoreController : Controller
            {
                MusicStoreEntities storeDB = new MusicStoreEntities();
            }


        }
    }
4

1 回答 1

2

在 StoreController 类中,您再次声明 StoreController,并在其中声明变量。你所做的是所谓的“内部类”,内部类与外部类不同,即使它看起来有相同的名称,它是全新的。

所以你打算这样做:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyMusicStore.Models;

namespace MyMusicStore.Controllers
{
    public class StoreController : Controller
    {
        //
        // GET: /Store/

        public ActionResult Index()
        {
            var genres = storeDB.Genres.ToList();
            return View(genres);
        }

        public ActionResult Browse(string genre)
        {
            var newGenre = new Genre { Name = genre };
            return View (newGenre);
        }

        public ActionResult Details(int id)
        {
            var album = new Album { Title = "Album" + id };
            return View(album);
        }

        MusicStoreEntities storeDB = new MusicStoreEntities();


    }
}
于 2013-02-06T05:06:57.343 回答