Hello,
I'm using .NET 3.5 SP1. I created entity 'Category', based on table 'category_table' and 'MyUser', based on table 'App_User_table'.
CREATE TABLE category_table (
catg_id int,
catg_name varchar(50),
catg_description varchar(250)
)
CREATE TABLE App_User_table (
userid int,
username varchar(50),
fullname varchar(50), catg_id int,
fullAddress varchar(200),
comments varchar(1000),CONSTRAINT FK_1 FOREIGN KEY (catg_id) REFERENCES Category_table (catg_id)
)
public class Category: System.Data.Objects.DataClasses.EntityObject{
public int CategoryId{get; set;}
public string CategoryName {get; set;}
....
}
public class AppUser : System.Data.Objects.DataClasses.EntityObject{
public int Uid {get; set;}
public string UserName {get; set;}
public string Name {get; set;}
public string Address {get; set;}
public string Comment {get; set;}
public Category category_table { .. }
public System.Data.Objects.DataClasses.EntityReference<Category> category_tableReference {...}
...........
}
I want to add and update AppUser entity in an ASP.NET MVC application.
For Add:
//create entity with passed values
AppUser user = new AppUser(){Uid=id, .... }
//Set EntityReference
user.category_tableReference.EntityKey = new System.Data.EntityKey("MyContext.CategorySet", "CategoryId", categoryIdSelected);
myContext.AddToCategorySet(user);
myContext.SaveChanges(true);
For Update:
//create entity with passed Id
AppUser user = new AppUser(){Uid=id }
//Attach entitymyContext.AttachTo("UserSet", user);
//Update entity with passed values
user.Address = addr;
....
//update selected CategoryId from DropDownList
user.category_tableReference.EntityKey = new System.Data.EntityKey("MyContext.CategorySet", "CategoryId", categoryId);
Update method does NOT update CategoryId in database.
Please tell, what is the best way to resolve the issue ?
Thank you.