我正在使用以下教程Click here for tutorial來創建註冊頁面。ASP.NET MVC 4數據庫實體錯誤
我已經遵循並執行了所有步驟,但有一個小錯誤,我不能擺脫。
TESTFYP1.MyDatabaseEntities'不包含一個定義‘用戶’和沒有擴展方法‘用戶’接受型的第一參數‘TESTFYP1.MyDatabaseEntities’可以找到(是否缺少使用指令或程序集參考?)
好心幫我...我下面給定教程的所有步驟。
//------------------------------------------------------------------------------
// <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]+)*)[email protected](([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);
}
}
}
有關您的代碼/模型的更多信息會很棒... – 2014-08-31 12:45:59