如何解决“运行所选代码生成器无法检索元数据时出错”错误?

时间:2016-10-17 19:10:48

标签: c# entity-framework

我是asp.net C#环境的新手,我收到了以下错误。

enter image description here

当我要打开我的项目的时候,这个错误就会出现。因为我无法继续我的工作。

而且,当我要添加视图的时间也会出现以下错误。

enter image description here

这是我的Controller(AccountController.cs)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TheFoody.Models;
using TheFoody.DataAccess;

namespace TheFoody.Controllers
{
    [Authorize]
    public class AccountController : Controller
    {

        //// GET: Account
        //public ActionResult Index()
        //{
        //    return View();
        //}

        // GET: /Account/Register
        [AllowAnonymous]
        public ActionResult Register()
        {
            return View();
        }

        // POST: /Account/Register
        [HttpPost]
        public ActionResult Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {

                using (TheFoodyContext db = new TheFoodyContext())
                {
                    if (db.Users.Any(u => u.email.Equals(model.Email)))
                    {
                        //TODO E.g. ModelState.AddModelError
                        ModelState.AddModelError("", "Email already exists");

                    }
                    else
                    {
                        User usr = new User();
                        usr.email = model.Email;
                        usr.fname = model.FirstName;
                        usr.lname = model.LastName;
                        usr.password = model.Password;
                        usr.status = "Active";
                        usr.user_type = "Admin";
                        usr.created_date = DateTime.Now;

                        db.Users.Add(usr);
                        db.SaveChanges();

                        Session["UserEmail"] = model.Email;
                        return RedirectToAction("Index", "Home");
                    }
                }

                return View(model);
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

        // GET: /Account/Login
        public ActionResult Login(string returnUrl)
        {
            ViewBag.ReturnUrl = returnUrl;
            return View();
        }

        [HttpPost]
        public ActionResult Login(LoginViewModel model, string returnUrl)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }
            using (TheFoodyContext db = new TheFoodyContext())
            {
                //var usr = db.Users.Single(u => u.email == model.Email && u.password == model.Password);
                var usr = db.Users.Where(u => u.email == model.Email && u.password == model.Password).FirstOrDefault();
                if (usr == null)
                {
                    ModelState.AddModelError("", "Invalid Email or password");
                }
                else
                {
                    Session["UserEmail"] = usr.email.ToString();
                    return RedirectToLocal(returnUrl);
                }

            }

            return View(model);
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult LogOff()
        {
            Session["UserEmail"] = null;
            return RedirectToAction("Index", "Home");
        }

        private ActionResult RedirectToLocal(string returnUrl)
        {
            if (Url.IsLocalUrl(returnUrl))
            {
                return Redirect(returnUrl);
            }
            return RedirectToAction("Index", "Home");
        }

        public ActionResult UpdateProfile()
        {
            return View();
        }

        [HttpPost]
        public ActionResult UpdateProfile(UpdateProfileViewModel model,string returnUrl)
        {
            return View(model);
        }

    }
}

这是我的模型类(AccountViewModel.cs)

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

namespace TheFoody.Models
{
    public class LoginViewModel
    {

        [Required]
        [Display(Name = "Email")]
        [EmailAddress]
        public string Email { get; set; }

        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }

        [Display(Name = "Remember me?")]
        public bool RememberMe { get; set; }
    }
    public class RegisterViewModel
    {

        [Display(Name = "FirstName")]
        public string FirstName { get; set; }

        [Display(Name = "LastName")]
        public string LastName { get; set; }

        [Required]
        [EmailAddress]
        [Display(Name = "Email")]
        public string Email { get; set; }

        [Required]
        [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Confirm password")]
        [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
        public string ConfirmPassword { get; set; } 
    }

    public class UpdateProfileViewModel
    {
        [Display(Name = "FirstName")]
        public string FirstName { get; set; }

        [Display(Name = "LastName")]
        public string LastName { get; set; }

        [Display(Name = "Phone")]
        public string Phone { get; set; }

        [Display(Name = "Photo")]
        public string Photo { get; set; }

        [Display(Name = "Address")]
        public string Address { get; set; }

        [Display(Name = "City")]
        public string City { get; set; }

        [Display(Name = "PostCode")]
        public int PostCode { get; set; }

        [Display(Name = "District")]
        public string District { get; set; }

    }
}

这是我的DbContext类。

    //------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace TheFoody.DataAccess
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;

    public partial class TheFoodyContext : DbContext
    {
        public TheFoodyContext()
            : base("name=TheFoodyContext")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public virtual DbSet<Menu> Menus { get; set; }
        public virtual DbSet<User> Users { get; set; }
        public virtual DbSet<Category> Categories { get; set; }
        public virtual DbSet<Restaurant> Restaurants { get; set; }
        public virtual DbSet<Restaurant_Type> Restaurant_Type { get; set; }

        //public System.Data.Entity.DbSet<TheFoody.Models.UpdateProfileViewModel> UpdateProfileViewModels { get; set; }
    }
}

所以我想为我的UpdateProfile Controller添加一个视图。但由于上述错误,我无法做到这一点。

0 个答案:

没有答案