TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Mouhssine tahri
NA
201
11.1k
MVC 5, Entity Framework 6 and Many to Many Relationship
Oct 13 2020 10:53 AM
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
// usr is list of user with userid=5
}
$.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
Reply
Answers (
6
)
Full Calendar events in asp.net mvc 5
link is not work inside the card