The introduction of EntityFramework to enumerations starts with version 5 (if not mistaken). Enumerations can greatly improve the readability of a program. So how do I use enumerations in EntityFramework's Codefirst mode? The answer is simple: still use it!
Seemingly nonsense, not actually, see below (Modify the user information definition in the previous article):
/// <summary> ///Gender Enumeration/// </summary> Public enumGender {Male, Female} Public classuserprofile {[Key] [databasegenerated (databasegeneratedoption.identity)] [Column (Order=0)] Public intUserId {Get;Set; } [Column (Order=1)] [Required] Public stringUserName {Get;Set; } [Column (Order=2)] Public stringUsercode {Get;Set; } [Column (Order=3)] Public intStatus {Get;Set; } [Column (Order=4)] Public stringEmail {Get;Set; } [Column (Order=5)] Public Gender Gender { get; Set ; } [Column (Order=6)] Public int? Creatorid {Get;Set; } Public VirtualIcollection<role> Roles {Get;Set; } Public int? Addressid {Get;Set; } [ForeignKey ("Addressid")] PublicAddress Address {Get;Set; } Publicuserprofile () {Gender=Framework.DomainModels.Gender.Male; } }
Delete the old database, rerun the project, and see what happens!
Doesn't seem to change anything? --------did not change much! Don't worry, keep looking down!
Add user edit view in AccountController to modify user's action.
/*load an action on the edit page*/ PublicActionResult Edituser (intID) {varModel = baseservice.getsingle<userprofile> (ID);//get user data by ID returnView (model); }/*Edit Page View*/@model framework.domainmodels.userprofile@{viewbag.title="Edituser";}@using (Html.BeginForm ()) {@Html. Editorformodel ();}
Run your project, enter the connection HTTP://LOCALHOST:****/ACCOUNT/EDITUSER/1 look at the results (note that the last 1 is the ID of a user you have registered, provided your routing configuration supports this access.) Is the default route configuration when you create a project).
OK, the enumeration variable name is already displayed, not the value of the int type we see in the database. EF is still smarter. In fact, this field should appear as a drop-down list, not a text box is the most reasonable.
some thought: for projects, not all fields that can be categorized are suitable for use with enum types. For example, the User state Status field, we can use the number 0 to indicate inactive, 1 table user is activated. To make our code look elegant, we then describe the field with an enumeration type. Of course there is nothing wrong with this, but if you consider extensibility, it is a mishap. If I add a "lock" to the user status, indicating that the user is logged in and can no longer log in anywhere else, the enumeration definition must be modified. In this case, it is better to use the normal int (or char) field. Although we need to make some judgments to display the user-readable status information, there are more benefits than modifying the type definition. We'll continue to discuss how to get the code to automatically recognize the fields of these int (or char), and show the more readable information to the user (I'm looking forward to it myself).