Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding & Updating Foreign key in Entity Framework v1

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.

like image 341
dev Avatar asked Apr 18 '26 01:04

dev


1 Answers

Here is the approach I would use for Update in an MVC app:

// Put the original Stub Entities for both the original User and its 
// original category. The second part is vital, because EF needs to know 
// original FK values to successfully do updates in 3.5 SP1
AppUser user = new AppUser {
    Uid = id, 
    Category = new Category {
        CategoryId = OriginalCategoryID
    }
};
ctx.AttachTo("UserSet", user);

// Then do this:
if (user.Category.CategoryId != categoryIdSelected)
{
    Category newCategory = new Category {CategoryId = CategoryIdSelected};
    // Attach because I assume the category already exists in the database
    ctx.AttachTo("CategorySet", newCategory);
    user.Category = newCategory;
}

// Update entity with passed values
user.Address = addr;
....

As you can see you need to be able to get OriginalCategoryID from somewhere, I recommend a hidden field on the form.

This will do the everything you need. Check out Tip 26 - How to avoid database queries using Stub Entities for more info on the Stub Entity trick.

Hope this helps

Alex

like image 137
Alex James Avatar answered Apr 20 '26 13:04

Alex James



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!