Copy this link when reproducing:
http://www.casperlee.com/en/y/blog/46
Since the database is ready, the next thing I'm going to do is to download the Entity Framework and see how it works. Before doing that, let's create all the projects first.
1. Run VS2015.
2. Select “File -> New -> Project…” to create a new project.
3. Select "Installed -> Templates -> Visual C# -> Windows -> Windows Forms Application"; type in "com.casperlee.ResourceManager" as the project name and the solution name; check the option of "Create directory for solution"; Click on the OK button.
Hint: the project name will also be the default namespace name in the project, so better to prefix the project name so that it is unique all over the world. And I believe using my private website domain as the prefix is the best way to achieve that.
4. The project "com.casperlee.ResourceManager" is successfully created now. For simplicity, I'll call it the main project or the UI project in the rest of this article.
Hint: UI is an abbreviation of "User Interface".
5. Rename "Form1.cs" to "MainForm.cs" and design the form as below.
6. In the Solution Explorer, right click on the solution and a menu will be popped up.
7. Select "Add -> New Project..." to create a new project.
8. Select "Installed -> Visual C# -> Windows -> Class Library"; type in "com.casperlee.ResourceManager.Dal" as the project name; click on the OK button.
Hint: Dal is an abbreviation of "Data access layer".
9. The project "com.casperlee.ResourceManager.Dal" is successfully created now. For simplicity, I'll call it the Dal project in the rest of this article.
10. Create the following projects using the same approach to create the Dal project.
The Bll project: com.casperlee.ResourceManager.Bll
Hint: Bll is an abbreviation of "Business logic layer".
The Library project: com.casperlee.Library
11. Right click on the main project and select "Properties" on the popped menu.
12. Switch to the "Build" tab, and modify the "Output path" to what ever you like, for instance, "..\Bin".
13. Do this for all the projects.
14. Right click on the main project, and select "Add -> Reference..." on the popped menu.
15. Switch to the "Projects" tab, and select "com.casperlee.ResourceManager.Bll" and "com.casperlee.ResourceManager.Dal", and then click on the OK button.
16. Use the same approach to add the references to the Bll project: "com.casperlee.ResourceManager.Dal" and "com.casperlee.Library".
17. Right click on the solution and select "Rebuild Solution" on the popped menu, and make sure all projects are compiled successfully.
Since all projects have been created now, let's download the Entity framework and see how it works.
18. Create a folder in the Bin folder and name it Data.
19. Copy the SQLite database file we created earlier to this Data folder.
20. Right click on the Dal project and select "Manage NuGet packages..." on the popped menu.
21. Switch to the "Browse" tab, search with the "SQLite", and then install the "System.Data.SQLite" library.
Hint: although there are several items in the list, we only need to install the first one which is a full package and includes everything we need here.
22. Wait for the install process to finish.
23. Go to the web page http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki
24. Search with "2015" and locate the related packages.
25. Download and run it.
26. Select your favorite path and click on the "Next >" button to continue.
27. Select all the items and click on "Next >" button to continue.
28. Uncheck the "Don't create a Start Menu folder", and click on the "Next >" button to continue.
29. Select all the items and click on the "Next >" button to continue.
30. Click on the "Install" button to install, and wait for the process to finish.
Ok, now that we've downloaded the Entity Framework, let's see how it works.
31. Right click on the Dal project, and select "Add -> New Items..." on the popped menu.
32. Select "Installed -> Visual C# Items -> Data -> ADO.NET Entity Data Model"; type in "ResourceManage" as the name; click on the "Add" button to continue.
33. Select "EF Designer from database" and click on "Next >" button to continue.
34. Click on the "New Connection.." button, then a dialog is shown as below.
35. Select "System.Data.SQLite.Database File" and click on the "OK" button to continue.
36. Click on the "Browse" button to select the SQLite database file in the Data folder, and then click "OK" button to continue.
37. Select all the objects you want to include; select the "Pluralize or singularize generated object names" option and the "Include foreign key columns in the model" option; click on the "Finish" button and wait for it to finish.
38. The model is created successfully. As we can see in the above image, it helps us to generate 2 classes: ResourceType and Resource, and it also helps us to link them together.
39. If we changed the database structure in the future, we need to regenerate / update the model using the menu shown in the above image.
40. Right click on the Bll project and select “Add -> Class…” to add a class ResourceTypeBll.cs.
41. Put the following code into the class.
using System.Collections.Generic;
using System.Linq;
using com.casperlee.ResourceManager.Dal;
namespace com.casperlee.ResourceManager.Bll
{
public class ResourceTypeBll
{
public static void InitializeResourceTypes()
{
List<ResourceType> types = new List<ResourceType>();
ResourceType type = new ResourceType();
type.Key = "DOC";
type.Name = "Document";
types.Add(type);
type = new ResourceType();
type.Key = "PIC";
type.Name = "Picture";
types.Add(type);
using (ResourceManageEntities rme = new ResourceManageEntities())
{
foreach (ResourceType rt in types)
{
if (rme.ResourceTypes.Where(x => x.Key == rt.Key).Count() == 0)
{
rme.ResourceTypes.Add(rt);
}
}
rme.SaveChanges();
}
}
}
}
42. Since we need to use the functions of the Entity Framework, we need to add a reference of "EntityFramework.dll" to the Bll project.
43. Find out the version of the "EntityFramework.dll" currently used by looking at the Dal project in the Solution Explorer.
44. Right click on the "EntityFramework" item and select "Properties" on the popped menu.
45. Remember the path of the EntityFramework.dll.
46. Add a reference of this dll to the Bll project.
47. Open the MainForm.cs in the main project.
48. Double click on the title bar to create an event handler for the Load event of the form. And put the following code in.
private void MainForm_Load(object sender, EventArgs e)
{
ResourceTypeBll.InitializeResourceTypes();
}
49. Merge the content of the "App.config" file in the Dal project to the "App.config" file in the main project.
Hint: during the merging, please make sure the <configSections> keeps in the first place under the <configuration> section.
50. Rebuild all the projects and run the application.
51. Close the application.
52. Run cmd.exe and go to the folder of database, and run the following command to see if the data is successfully inserted.
53. It works. Yeah!