[RESOLVED]from data in a class to a database

To get more familiar with MVC, I worked through the exercise in asp.net/mvc/overview/older-versions/creating-a-mvc-3-application-with-razor-and-unobtrusive-javascript. In this exercise, there is a Users class which substitutes for a database:

public class Users {
	public Users() {
		_userList.Add(new UserModel
			{
				UserName = "queene",
				FirstName = "Queen",
				LastName = "Elizabeth",
				City = "Buckingham"
			});
		_userList.Add(new UserModel
			{
				UserName = "annb",
				FirstName = "Ann",
				LastName = "Beebe",
				City = "Boston"
			});
					
	}

	public List<UserModel> _userList = new List<UserModel>();

	public void Update(UserModel umToUpdate)
	{
		foreach(UserModel um in _userList)
		{
			if(um.UserName == umToUpdate.UserName)
			{
				_userList.Remove(um);
				_userList.Add(umToUpdate);
				break;
			}
		}
	}

	public void Create(UserModel umToUpdate)
	{
		foreach(UserModel um in _userList)
		{
			if (um.UserName == umToUpdate.UserName)
				throw new System.InvalidOperationException("Duplicate username: " + um.UserName);
		}
		_userList.Add(umToUpdate);
	}

	public void Remove(string userName)
	{
		foreach (UserModel um in _userList)
			if (um.UserName == userName)
			{
				_userList.Remove(um);
				break;
			}
	}

	public UserModel GetUser(string uid)
	{
		UserModel userModel = null;
		foreach (UserModel um in _userList)
			if (um.UserName == uid)
				userModel = um;
		return userModel;
	}

}

I also created an EF context based on a database table. I want to convert the little project over to using the database but I’m not sure how to set it up. I went through another practice site (asp.net/mvc/overview/getting-started/database-first-development/creating-the-web-application),
which was more concerned with scaffolding the DB into the project. The problem with that is I believe the scaffolding menus referred to there were for .Net 4.5 and I work with 4.0, so it got kind of confusing. Anyway, does anyone have any sources or suggestions
that might help me out? (scaffolding or not) Let me know if there’s any more code you need to see. Thanks in advance for your help.

I don’t know if it makes a difference anywhere else but on the Index view, but I’m using Razor.

Hi muybn,

To learn MVC with EF, please refer to:

# Models and Data Access

http://www.asp.net/mvc/overview/older-versions/mvc-music-store/mvc-music-store-part-4

# Getting Started with Entity Framework 6 Code First using MVC 5

http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application

Best Regards

Starain

Thank you, Starain. I’m sure these references will be helpful.

Leave a Reply