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
Adnan Okko
NA
19
3.3k
Entity Framework one to many relation not updated
Mar 22 2018 12:06 PM
I have the following entities:
BIEntities db =
new
BIEntities();
public
class
SALES
{
public
int
IDs {
get
;
set
;}
public
string
product {
get
;
set
; }
ICollection<COUNTRIES> COUNTRIES {
get
;
set
; }
}
public
class
COUNTRIES
{
public
int
IDc {
get
;
set
; }
public
string
country {
get
;
set
; }
public
virtual
SALES SALES {
get
;
set
; }
}
And a ViewModel:
public
class
myViewModel
{
public
int
ID {
get
;
set
; }
public
string
product {
get
;
set
; }
List<
string
> country {
get
;
set
; }
}
I am trying to update (edit) all entities from myViewModel:
SALES sUpdate = db.SALES.FirstOrDefault(s => s.IDs == myModel.ID);
if
(sUpdate !=
null
)
{
//This works fine!
db.Entry(sUpdate).CurrentValues.SetValues(myModel);
}
var cUpdate = from c
in
db.COUNTRIES where c.IDc == myModel.ID select c;
if
(cUpdate !=
null
)
{
foreach
(var item
in
cUpdate)
{
item.country = myModel.country;
item.IDc = myModel.ID;
//What should I add here to update the records??
}
}
db.SaveChanges();
This can update the main table "SALES" but table "COUNTRIES" is not updated.
Reply
Answers (
0
)
convert datatable to pivot table
Will it work as crud operation?