I've two model classes:
- public Service()
- public int CodeService { get; set; }
- public string DesignationService { get; set; }
- public virtual ICollection<User> ServiceToUser { get; set; }
- public User()
- public int UserID { get; set; }
- public string FirstName { get; set; }
- public virtual ICollection<Service> UserToService { get; set; }
- }
The above code generates me 3 tables Service, User & ServiceUsers(autogenerated by EF)
i want insert in UserID = 5 and CodeService = 8 in ServiceUsers table.()
values 5 already exist in table User
values 8 already exist in table Service
i use webapi
- var apiurl = "http://localhost:14405/api/ServiceApi";
- var data = {
- DesignationService: $("#description").val(),
- serviceToUser: user
- }
-
- $.ajax({
- url: apiurl,
- type: 'POST',
- dataType: 'json',
- data: data,
- success: function (d) {
- noty({ text: 'Association réussie', layout: 'topRight', type: 'success'
- });
- },
- error: function () {
- noty({ text: 'Association échouée', layout: 'topRight', type: 'error' });
- }
- });
-
- public void addService(Service service)
- {
- Attach(service);
- bd.Service.Add(service);
- bd.SaveChanges();
- }
- private void Attach(Service service)
- {
- int l = service.ServiceToUser.Count();
- for (int i = 0; i < l; i++)
- {
- bd.User.Attach(service.ServiceToUser.ElementAt<User>(i));
- }
- }
but when addService is executed it create a new record in service table value 6 and in ServiceUsers table it create 6 for code service and 5 in userid.
can you help me to solve this problem.
I want only insert ServiceUsers
thanks