问题:无法引用 - 当前上下文中不存在“Compeditors”这个名称

时间:2014-01-22 22:14:21

标签: c# asp.net-mvc asp.net-mvc-4 interface

错误:名称'Compeditors'在当前上下文中不存在

新手MVC 4并试图获得完整的正确方法来完成它。使用MVC arch我也使用了一个界面。在我尝试将数据添加到数据库之前,一切都很顺利。我有一个数据添加视图,控制器接受视图和模型以按预期将数据导入数据库。有2个名称空间。 eManager.Core和eManager.Web2

只是尝试了我能想到的一切,但是无法找到Compeditors系列..任何想法都会大大减少!

//Compeditor Class in eManager.Core -- Compeditor.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;



namespace eManager.Core
{
    public class Compeditor
    {
        [Key]
        public virtual int CompeditorId { get; set; }
        public virtual string FirstName  { get; set; }
        public virtual string LastName { get; set; }
        public virtual string MiddleInt { get; set; }
        public virtual string StreetAddress { get; set; }
        public virtual string City { get; set; }
        public virtual string State { get; set; }
        public virtual string PostalCode { get; set; }
        public virtual string EmailAddress { get; set; }
        public virtual string HomePhone { get; set; }
        public virtual string CellPhone { get; set; }
        public virtual int Height { get; set; }
        public virtual int Weight { get; set; }

    }
}

//DB Access Setup - BodyBuilderDB.cs  in eManager.Web2

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using eManager.Core;


namespace eManager.Web2.Infastructure
{
    public class BodyBuilderDB : System.Data.Entity.DbContext, IBodyBuilderDataSource
    {

        public BodyBuilderDB() : base("DefaultConnection")
        {

        }


        public DbSet<Class_Type> Class_Types { get; set; }
        public DbSet<Compeditor> Compeditors { get; set; }
        public DbSet<Event> Events { get; set; }
        public DbSet<Event_Class> Event_Classes { get; set; }


        IQueryable<Compeditor> IBodyBuilderDataSource.Compeditors
        {
            get
            { return Compeditors; }


        }

        IQueryable<Event_Class> IBodyBuilderDataSource.Event_Classes
        {
            get
            { return Event_Classes; }

        }

        IQueryable<Event> IBodyBuilderDataSource.Events
        {
            get
            { return Events; }

        }

        IQueryable<Class_Type> IBodyBuilderDataSource.Class_Types
        {
            get
            { return Class_Types; }

        }

        void IBodyBuilderDataSource.Save()
        {
            SaveChanges();
        }

    }
}

//CompeditorController.cs -- Area I am having the issue in eManager.Web2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using eManager.Core;
using eManager.Web2.Models;


namespace eManager.Web2.Controllers
{
    public class CompeditorController : Controller
    {
        private readonly IBodyBuilderDataSource _db;
        public CompeditorController(IBodyBuilderDataSource db)
        {
            _db = db;
        }

        [HttpGet]
        public ActionResult Create()
        {
            var model = new CreateCompeditorViewModel();
            return View();
        }

        [HttpPost]
        public ActionResult Create(CreateCompeditorViewModel viewModel)
        {
            var compeditor = new Compeditor();

            {
                if (ModelState.IsValid)
                compeditor.CompeditorId = viewModel.CompeditorId;
                compeditor.FirstName = viewModel.FirstName;
                compeditor.MiddleInt = viewModel.MiddleInt;
                compeditor.LastName = viewModel.LastName;
                compeditor.StreetAddress = viewModel.StreetAddress;
                compeditor.City = viewModel.City;
                compeditor.State = viewModel.State;
                compeditor.PostalCode = viewModel.PostalCode;
                compeditor.HomePhone = viewModel.HomePhone;
                compeditor.CellPhone = viewModel.CellPhone;
                compeditor.Height = viewModel.Height;
                compeditor.Weight = viewModel.Weight;
                compeditor.EmailAddress = viewModel.EmailAddress;

                Compeditors.add(compeditor);  // "The name 'Compeditors' does not exsist in the current context

                _db.Save();

            }

            return View(viewModel);
        }


    }
}

//IBodyBuilderDataSource:  the Interface in eManager.Core
using eManager.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace eManager.Core

{
    public interface IBodyBuilderDataSource
    {
        IQueryable<Compeditor> Compeditors { get; }
        IQueryable<Event_Class> Event_Classes { get;  }
        IQueryable<Event> Events { get;  }
        IQueryable<Class_Type> Class_Types { get;  }
        void Save();
    }
}

//CreateCompeditorViewModel.cs -- View of the data add

using eManager.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace eManager.Core

{
    public interface IBodyBuilderDataSource
    {
        IQueryable<Compeditor> Compeditors { get; }
        IQueryable<Event_Class> Event_Classes { get;  }
        IQueryable<Event> Events { get;  }
        IQueryable<Class_Type> Class_Types { get;  }
        void Save();
    }
}

1 个答案:

答案 0 :(得分:0)

你说你的错误就在这一行(在你的控制器中):

Compeditors.add(compeditor); 

但是,“Compeditors”在BodyBuilderDB(您的控制器中的_db字段)中已经过贴标签。所以你想要的可能是:

_db.Compeditors.add(compeditor);