1

I am using a following tutorial Click here for tutorial to create a registration page.

I have followed and performed all the step but there is a small error that i cant get rid off.

TESTFYP1.MyDatabaseEntities' does not contain a definition for 'Users' and no extension method 'Users' accepting a first argument of type 'TESTFYP1.MyDatabaseEntities' could be found (are you missing a using directive or an assembly reference?)

Kindly Help me ... I am following all the step in the given tutorial.

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

}

This is the code where i am getting error

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);
        }
    }
}
1
  • a little more info about your code/model would be great... Commented Aug 31, 2014 at 12:45

2 Answers 2

1

You probably defined the class User but you didn't added the Property Users to the class MyDatabaseEntities (your model) you should have something like public partial class MyDatabaseEntities: DbContext { public DbSet<User> Users { get; set; } }

either you can change your code in HomeController : dc.Users.Add(U); Or change in your edmx file the name of the entities from Table1 to Users. This corresponds to step 4. I don't know why the name of the entities ended up with "Table1"

Sign up to request clarification or add additional context in comments.

5 Comments

Hows that possible by following all steps in the tutorial? anyways can u please tell me how to do that? :)
Can you update your post and add a printscreen of your model or something like that (edmx file)? Difficult to tell you which step you made wrong without seeing the code (every step of the tutorial seems great to achieve the goal).
I have added the code from edmx file kindly check it :)
Your Users have a DbSet called "Table1" apparently. Change dc.Users.Add(U); by dc.Table1.Add(U); might do the job
(NEW ERROR)Error 1 'System.Data.Objects.ObjectSet<TESTFYP1.Table1>' does not contain a definition for 'Add' and no extension method 'Add' accepting a first argument of type 'System.Data.Objects.ObjectSet<TESTFYP1.Table1>' could be found (are you missing a using directive or an assembly reference?)
0

This is the database context. when we add entity model (database first) the data context created automatically. here In this application, the database name is "MyDatabase" that's why it's created the data context in this name MyDatabaseEntities. check once what is your data context name and use the datacontext in case of MyDatabaseEntities.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.