'System.InvalidOperationException'类型的异常出现在EntityFramework.dll但在用户code,而注册没有处理 [英] An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code while Registering

查看:157
本文介绍了'System.InvalidOperationException'类型的异常出现在EntityFramework.dll但在用户code,而注册没有处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到我的code以下异常。


  

'System.InvalidOperationException'类型的异常出现在EntityFramework.dll但在用户code没有处理。


  
  

其他信息:实体类型用户不是该机型为当前上下文的一部分


以下是我的注册控制器。

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用System.Web.Mvc;命名空间Registration2.Controllers
{
    公共类UserController的:控制器
    {
        // GET:用户
        公众的ActionResult指数()=>视图();
        公众的ActionResult寄存器()=>视图();        [HttpPost]
        [ValidateAntiForgeryToken]
        公众的ActionResult寄存器(Registration2.user U)
        {
            如果(ModelState.IsValid)
            {
                使用(MyDatabase2Entities DC =新MyDatabase2Entities())
                {
                    //你应该检查这里重复登记
                    // dc.users.Add                    dc.users.Add(U);
                    dc.SaveChanges();
                    ModelState.Clear();
                    U = NULL;
                    ViewBag.Message =成功注册完成;
                }
            }
            返回查看(U);
        }
    }
}

我完全新ASP.net和MVC所以请告诉如果我错过了什么,我将它张贴在这里。


解决方案

  

的实体类型用户不是该机型为当前上下文的一部分。


这是显着的部分。是您MyDatabase2Entities上下文的用户对象的一部分?这似乎并非如此。

I am getting following exception in my code.

An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code.

Additional information: The entity type user is not part of the model for the current context.

Following is my Register Controller.

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

namespace Registration2.Controllers
{
    public class userController : Controller
    {
        // GET: user
        public ActionResult Index() => View();
        public ActionResult Register() => View();

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Register(Registration2.user U)
        {
            if (ModelState.IsValid)
            {
                using (MyDatabase2Entities dc = new MyDatabase2Entities())
                {
                    //you should check duplicate registration here 
                    // dc.users.Add

                    dc.users.Add(U);
                    dc.SaveChanges();
                    ModelState.Clear();
                    U = null;
                    ViewBag.Message = "Successfully Registration Done";
                }
            }
            return View(U);
        }
    }
}

I am totally new to ASP.net and MVC so please tell if i miss anything, i will post it here.

解决方案

The entity type user is not part of the model for the current context.

This is the salient part. Is the User object part of your MyDatabase2Entities context? It appears not.

这篇关于'System.InvalidOperationException'类型的异常出现在EntityFramework.dll但在用户code,而注册没有处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆