C# ASP.NET MVC 4数据库实体错误

C# ASP.NET MVC 4数据库实体错误,c#,asp.net,asp.net-mvc,asp.net-mvc-4,registration,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,Registration,我正在使用以下教程创建注册页面 我遵循并执行了所有步骤,但有一个小错误,我无法摆脱 TESTFYP1.MyDatabaseEntities”不包含“用户”的定义,并且找不到接受类型为“TESTFYP1.MyDatabaseEntities”的第一个参数的扩展方法“用户”(是否缺少using指令或程序集引用?) 请帮助我。。。我将遵循给定教程中的所有步骤 //----------------------------------------------------------------------

我正在使用以下教程创建注册页面

我遵循并执行了所有步骤,但有一个小错误,我无法摆脱

TESTFYP1.MyDatabaseEntities”不包含“用户”的定义,并且找不到接受类型为“TESTFYP1.MyDatabaseEntities”的第一个参数的扩展方法“用户”(是否缺少using指令或程序集引用?)

请帮助我。。。我将遵循给定教程中的所有步骤

//------------------------------------------------------------------------------
// <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>
//------------------------------------------------------------------------------

using System;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Data.EntityClient;
using System.ComponentModel;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

[assembly: EdmSchemaAttribute()]

namespace TESTFYP1
{
    public partial class User
    {
        public int UserID { get; set; }
        [Required(ErrorMessage = "Please provide username", AllowEmptyStrings = false)]
        public string Username { get; set; }
        [Required(ErrorMessage = "Please provide Password", AllowEmptyStrings = false)]
        [DataType(System.ComponentModel.DataAnnotations.DataType.Password)]
        [StringLength(50, MinimumLength = 8, ErrorMessage = "Password must be 8 char long.")]
        public string Password { get; set; }
        [Compare("Password", ErrorMessage = "Confirm password dose not match.")]
        [DataType(System.ComponentModel.DataAnnotations.DataType.Password)]
        public string ConfirmPassword { get; set; }
        [Required(ErrorMessage = "Please provide full name", AllowEmptyStrings = false)]
        public string FullName { get; set; }

        [RegularExpression(@"^([0-9a-zA-Z]([\+\-_\.][0-9a-zA-Z]+)*)+@(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]*\.)+[a-zA-Z0-9]{2,3})$",
            ErrorMessage = "Please provide valid email id")]
        public string EmailID { get; set; }
    }
    #region Contexts

    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    public partial class MyDatabaseEntities : ObjectContext
    {
        #region Constructors

        /// <summary>
        /// Initializes a new MyDatabaseEntities object using the connection string found in the 'MyDatabaseEntities' section of the application configuration file.
        /// </summary>
        public MyDatabaseEntities() : base("name=MyDatabaseEntities", "MyDatabaseEntities")
        {
            this.ContextOptions.LazyLoadingEnabled = true;
            OnContextCreated();
        }

        /// <summary>
        /// Initialize a new MyDatabaseEntities object.
        /// </summary>
        public MyDatabaseEntities(string connectionString) : base(connectionString, "MyDatabaseEntities")
        {
            this.ContextOptions.LazyLoadingEnabled = true;
            OnContextCreated();
        }

        /// <summary>
        /// Initialize a new MyDatabaseEntities object.
        /// </summary>
        public MyDatabaseEntities(EntityConnection connection) : base(connection, "MyDatabaseEntities")
        {
            this.ContextOptions.LazyLoadingEnabled = true;
            OnContextCreated();
        }

        #endregion

        #region Partial Methods

        partial void OnContextCreated();

        #endregion

        #region ObjectSet Properties

        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        public ObjectSet<Table1> Table1
        {
            get
            {
                if ((_Table1 == null))
                {
                    _Table1 = base.CreateObjectSet<Table1>("Table1");
                }
                return _Table1;
            }
        }
        private ObjectSet<Table1> _Table1;

        #endregion
        #region AddTo Methods

        /// <summary>
        /// Deprecated Method for adding a new object to the Table1 EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
        /// </summary>
        public void AddToTable1(Table1 table1)
        {
            base.AddObject("Table1", table1);
        }

        #endregion
    }


    #endregion

    #region Entities

    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    [EdmEntityTypeAttribute(NamespaceName="MyDatabaseModel", Name="Table1")]
    [Serializable()]
    [DataContractAttribute(IsReference=true)]
    public partial class Table1 : EntityObject
    {
        #region Factory Method

        /// <summary>
        /// Create a new Table1 object.
        /// </summary>
        /// <param name="userID">Initial value of the UserID property.</param>
        /// <param name="username">Initial value of the Username property.</param>
        /// <param name="password">Initial value of the Password property.</param>
        /// <param name="fullName">Initial value of the FullName property.</param>
        public static Table1 CreateTable1(global::System.Int32 userID, global::System.String username, global::System.String password, global::System.String fullName)
        {
            Table1 table1 = new Table1();
            table1.UserID = userID;
            table1.Username = username;
            table1.Password = password;
            table1.FullName = fullName;
            return table1;
        }

        #endregion
        #region Primitive Properties

        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
        [DataMemberAttribute()]
        public global::System.Int32 UserID
        {
            get
            {
                return _UserID;
            }
            set
            {
                if (_UserID != value)
                {
                    OnUserIDChanging(value);
                    ReportPropertyChanging("UserID");
                    _UserID = StructuralObject.SetValidValue(value);
                    ReportPropertyChanged("UserID");
                    OnUserIDChanged();
                }
            }
        }
        private global::System.Int32 _UserID;
        partial void OnUserIDChanging(global::System.Int32 value);
        partial void OnUserIDChanged();

        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
        [DataMemberAttribute()]
        public global::System.String Username
        {
            get
            {
                return _Username;
            }
            set
            {
                OnUsernameChanging(value);
                ReportPropertyChanging("Username");
                _Username = StructuralObject.SetValidValue(value, false);
                ReportPropertyChanged("Username");
                OnUsernameChanged();
            }
        }
        private global::System.String _Username;
        partial void OnUsernameChanging(global::System.String value);
        partial void OnUsernameChanged();

        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
        [DataMemberAttribute()]
        public global::System.String Password
        {
            get
            {
                return _Password;
            }
            set
            {
                OnPasswordChanging(value);
                ReportPropertyChanging("Password");
                _Password = StructuralObject.SetValidValue(value, false);
                ReportPropertyChanged("Password");
                OnPasswordChanged();
            }
        }
        private global::System.String _Password;
        partial void OnPasswordChanging(global::System.String value);
        partial void OnPasswordChanged();

        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
        [DataMemberAttribute()]
        public global::System.String FullName
        {
            get
            {
                return _FullName;
            }
            set
            {
                OnFullNameChanging(value);
                ReportPropertyChanging("FullName");
                _FullName = StructuralObject.SetValidValue(value, false);
                ReportPropertyChanged("FullName");
                OnFullNameChanged();
            }
        }
        private global::System.String _FullName;
        partial void OnFullNameChanging(global::System.String value);
        partial void OnFullNameChanged();

        #endregion

    }

    #endregion

}

您可能定义了类用户,但没有将属性用户添加到类MyDatabaseEntities(您的模型) 您应该有类似于公共部分类MyDatabaseEntities:DbContext的内容 { 公共数据库集用户{get;set;} }

您可以在HomeController:dc.Users.Add(U)中更改代码; 或者将edmx文件中的实体名称从表1更改为用户。
这对应于步骤4。我不知道为什么实体的名称以“Table1”结尾这是数据库上下文。当我们添加实体模型(首先是数据库)时,会自动创建数据上下文。在此应用程序中,数据库名称为“MyDatabase”,这就是为什么它以MyDatabaseEntities的名称创建数据上下文。检查一下您的数据上下文名称,并在MyDatabaseEntities的情况下使用datacontext。

关于您的代码/模型的更多信息将非常有用……按照本教程中的所有步骤如何实现这一点?不管怎样,你能告诉我怎么做吗?:)你能更新你的帖子并添加模型的打印屏幕或类似的东西(edmx文件)吗?很难在看不到代码的情况下告诉您错误的步骤(教程的每一步似乎都很好地实现了目标)。我已经从edmx文件中添加了代码,请检查:)您的用户显然有一个名为“Table1”的数据库集。更改dc.Users.Add(U);由dc.表1.添加(U);可能执行作业(新错误)错误1“System.Data.Objects.ObjectSet”不包含“Add”的定义,并且找不到接受“System.Data.Objects.ObjectSet”类型的第一个参数的扩展方法“Add”(是否缺少using指令或程序集引用?)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace TESTFYP1.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Index(User U)
        {
            if (ModelState.IsValid)
            {
                using (MyDatabaseEntities dc = new MyDatabaseEntities())
                {
                    dc.Users.Add(U); //Error i em telling
                    dc.SaveChanges();
                    ModelState.Clear();
                    U = null;
                    ViewBag.Message = "Successfully Registration Done";
                }
            }
            return View(U);
        }
    }
}