2014-08-31 68 views
1

我正在使用以下教程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&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 

} 

這是我收到

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

有關您的代碼/模型的更多信息會很棒... – 2014-08-31 12:45:59

回答

1

你可能定義的類用戶錯誤的代碼,但你沒有添加的屬性用戶對類MyDatabaseEntities(模型) 你應該有類似public partial class MyDatabaseEntities: DbContext { public DbSet<User> Users { get; set; } }

要麼你可以在HomeController中更改你的代碼:dc.Users.Add(U); 或者在您的edmx文件中將Table1中實體的名稱更改爲Users。 這對應於步驟4.我不知道爲什麼實體的名稱以「Table1」結尾了

+0

按照教程中的所有步驟操作,可以實現嗎?反正可以請你告訴我該怎麼做? :) – 2014-08-31 12:50:50

+0

你可以更新你的文章,並添加模型或類似的東西(EDMX文件)的打印屏幕? 很難告訴你在沒有看到代碼的情況下你做錯了哪一步(教程的每一步看起來都很棒,以達到目標)。 – 2014-08-31 12:53:04

+0

我添加了edmx文件中的代碼,請檢查它:) – 2014-08-31 13:09:04

0

這是數據庫上下文。當我們添加實體模型(數據庫優先)時自動創建數據上下文。這裏在這個應用程序中,數據庫名稱是「MyDatabase」,這就是爲什麼它以MyDatabaseEntities這個名稱創建數據上下文的原因。檢查一次你的數據上下文名稱,並在MyDatabaseEntities的情況下使用datacontext。