Mvc+ef of multiple tables associated with data updates

Source: Internet
Author: User

Multi-table associated data Update
There are two kinds of association data: one is a pair, one is always many to many
Eg: Suppose that a user can have any number of roles, and a role can have any number of users. The following is the implementation of editing a user, which displays the user's role for editing.
Steps:
1. First add a ViewModel to indicate whether the role is assigned to a user.
2. Open Userrolecontroller and add an edit action to display the edit page.
3. Open views\userrole\index.cshtml, add an Edit button
4. Automatically generate edit View based on edit action
5. Modify the relevant content, mainly two points: one is the department, the second city role (the role is displayed through a set of checkboxes. The checkbox displays all roles in the database, and the selected status is displayed for the user. Update the user role by ticking the checkbox)
Note: The role of a few such a problem, if more classic practice, you can use two ListBox, the middle with arrows to move the left and right options
6. Finally complete the HttpPost edit function
A. Update the Sysuser table first: Update entity:usertoupdate with the values in model binder. As you can see, we used a whitelist to specify the fields in the database that need to be updated.
TryUpdateModel (Usertoupdate, "", new string[] {"LoginName", "Email", "Password", "CreateDate", "Sysdepartmentid"})
B. Update the Sysuserrole table: Compare the values in the database with the edited values, the basic logic is: if it is selected, the original is not to be added, if not selected, the original has to be deleted.
Updateuserroles (Selectedroles, usertoupdate);
Note: In Updateuserroles, I created a new connection using (Accountcontext db2=new accountcontext ()) if the previous DB would report the following error: an open Command associated DataReader, it must be closed first.
Summary of native SQL usage
A. Native SQL execution query:
Need to return entity model, use Dbset.sqlquery (context Trace, equivalent to LINQ mode)
Need to return other types, using Database.sqlquery
B. Native SQL to perform the update:
Using Database.executesqlcommand

MVC+EF associated data Update for multiple tables

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.