Adding and updating a foreign key in Entity Framework v1

I am using .NET 3.5 SP1. I created a Category entity, based on the table category_table and MyUser, based on the 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 an AppUser object in an ASP.NET MVC application.

For adding:

//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);

To 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 the selected CategoryId from DropDownList

user.category_tableReference.EntityKey = new System.Data.EntityKey("MyContext.CategorySet", "CategoryId", categoryId);

The update method does NOT update the CategoryId in the database.

Tell me, what is the best way to solve the problem?

Thanks.

+3
source share
2 answers

Here is the approach I would use to upgrade in an MVC application:

// 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;
....

, OriginalCategoryID -, .

, . 26 - Stub Entities Stub Entity.

,

+8

: . - - , "EntityKey", , .

1 Entity Framework - "" "" AppUser:

//create entity with passed values
AppUser user = new AppUser(){Uid=id, ....  }

// set the Category navigation property
Category newUserCategory  = Category.GetbyID(4); // or whatever you need
user.Category = newUserCategory;

myContext.AddToCategorySet(user);
myContext.SaveChanges(true);

EF v4, .NET 4.0 (2009), , .

:

- . a >

+1

Source: https://habr.com/ru/post/1714943/


All Articles