It should be something simple, it should not be ensured why I did not find the solution on the Web. Maybe it's specific for EF 6 or ASP detection or, maybe I'm entering the wrong search key word.
To make things easy, let me have two model class teachers and children; A child can be assigned to only one teacher, but a teacher can have many children. This is a multi-user environment, so each child and every teacher is related to a specific user. As I am using the code earlier, my database is made up of these model sections:
public class children [[required] Public Ent ID {get; Set; } [Required] Public string name {get; Set; } [Required] Public Virtual Teacher {Receive; Set; } [Required] Public Virtual Application User User {Received; Set; }} Public Class Teacher {{Required] Public Ent ID {get; Set; } [Required] Public string name {get; Set; } Public virtual iconging & lt; Children & gt; Children {receive; Set; } [Required] Public Virtual Application User User {Received; Set; }}
I have a view to add a new child: text box for the child's name; Dropdown with a list of teachers
Therefore, I am specifically creating a data transfer object for that scene:
public class AddNewKidViewDTO {public string name {get; Set;} Public IEnumerable & lt; SelectListItem & gt; Teachers {receive; Set; } Public Ink Selected Teacher Teacher {Receive; Set; }}
The form gets deposited and I get the AddNewKidViewDTO model of my population.
Now, I need to include a new child in the database. Therefore, I map the AddNewKidViewDTO properties to my child model properties one by one:
Public zero Insert (AddNewKidViewDTO addNewKidViewDTO) {Children baby = new child (); children. Name = AddNewKidViewDTO.Name; // Here comes the question how do I install a child? Teachers and children the user? }
UPDATE 1
I do not know how to get the selected teacher ID, I should know how the navigation property model object (using that ID) To assign to
2 update:
If I find an existing user object like this:
ApplicationUser user = usermanager.FindById (User.Identity.GetUserId & Lt; int & gt; ());
And then attach it to the child:
Kid.User = user; When a child joins, this throws an exception: The identity member 'mynamespace.DAL.Kid_User' does not exist in the metadata archive.
Any help appreciated.
enter the public zero InsertNewKid {AddNewKidViewDTO addNewKidViewDTO} {child = baby =); children. Name = AddNewKidViewDTO.Name; child. Teacher = Your Datacontext Tester Search (AddNewKidViewDTO.SelectedTeacherId); // reference Can be done either here or in your action link.
You should be able to do this with the user that you know the appropriate ID. If you do not have the ID in hand, then it is also possible to find this method here. Example:
child.user = context.supers.where (x => x.Name == "whatever string"). FirstOstrograph & lt; Users & gt; ();
This can be done with any attribute.
In keeping with its database, many relationships from 1 to the right have been prepared, the child himself "parent" teacher collection of children.
However for [essential]
public virtual Ilkonnak
A child is dependent on the existence of a teacher, but I do not really see the issue of dependence in both ways.
Teacher constructor should be added in the correct model category:
public teacher () {this.Kids = new hashset & lt; Children & gt; (); }
No comments:
Post a Comment