我正在使用以下教程Click here for tutorial来创建注册页面。
我已经跟踪并执行了所有步骤但是有一个小错误,我无法摆脱。
TESTFYP1.MyDatabaseEntities'不包含'Users'的定义,也没有扩展方法'用户'可以找到类型'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<T> 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
}
这是我收到错误的代码
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);
}
}
}
答案 0 :(得分:1)
您可能已经定义了User类,但您没有将属性用户添加到MyDatabaseEntities类(您的模型)
你应该有public partial class MyDatabaseEntities: DbContext
{
public DbSet<User> Users { get; set; }
}
要么你可以在HomeController中更改你的代码:dc.Users.Add(U); 或者在您的edmx文件中更改从Table1到Users的实体名称。 这对应于第4步。我不知道为什么实体的名称最终以&#34;表1&#34;
答案 1 :(得分:0)
这是数据库上下文。当我们添加实体模型(数据库优先)时,自动创建数据上下文。 here在此应用程序中,数据库名称为“MyDatabase”,这就是为什么它以此名称MyDatabaseEntities创建数据上下文的原因。检查一下您的数据上下文名称是什么,并在MyDatabaseEntities的情况下使用datacontext。