Entity framework 加入&;更新实体框架v1中的外键
我使用的是.NET3.5SP1。 我根据表“Category\u table”和表“App\u User\u table”创建了实体“Category”Entity framework 加入&;更新实体框架v1中的外键,entity-framework,foreign-keys,Entity Framework,Foreign Keys,我使用的是.NET3.5SP1。 我根据表“Category\u table”和表“App\u User\u table”创建了实体“Category” 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), ful
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 {...}
...........
}
更新:
//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);
//create entity with passed Id
AppUser user = new AppUser(){Uid=id }
//Attach entitymyContext.AttachTo("UserSet", user);
//Update entity with passed values
user.Address = addr;
....
//从DropDownList更新所选类别ID
user.category_tableReference.EntityKey = new System.Data.EntityKey("MyContext.CategorySet", "CategoryId", categoryId);
Update方法不更新数据库中的CategoryId
请告诉我,解决问题的最佳方法是什么?
谢谢。更新:请参阅Craig Stuntz和Alex James的评论-我已更正-似乎可以简单地更新“EntityKey”以实现您的要求。我在这里的帖子不再适用了 在版本1中,Entity Framework无法仅插入外键值-您需要将“Category”对象实例添加到“AppUser”的Category导航属性:
//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);
EFV4将在今年晚些时候(2009年)与.NET4.0一起发布,您将能够只设置外键值,以实现两个实体之间的关联
请参见此处的更多信息:
Marc以下是我在MVC应用程序中更新时使用的方法:
// 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,我建议在表单上设置一个隐藏字段
这会满足你所需要的一切。查看有关存根实体技巧的更多信息
希望这有帮助
亚历克斯那不是真的。除了Alex的“存根对象”建议外,您还可以仅使用ID新建EntityKey并将其分配给引用。Alex的建议与我的建议基本相同-您只需要创建一个“完整”类别对象-您不能只将值“4”分配给外键列-您需要提供一个“类别”反对。亚历克斯的建议不涉及数据库读取。你的建议包括阅读。那是不一样的。要分配EntityKey,您可以执行(猜测名称)user.CategoryReference.EntityKey=newEntityKey(“MyEntities.Categories”,“Cid”,4)@克雷格:当然,我同意——亚历克斯的建议比较圆滑,但事实是——你必须提供一个完整的“类别”对象。我将尝试您的EntityKey方法-我不知道这在EFv1中有效-我将尝试一下!当然,您也可以使用EntityKey方法。唯一的问题是,对于更新,在将其更改为最新值之前,还需要指定原始EntityKey。在3.5中,关系(即PK-FK对)是并发检查的一部分,因此为了更改一端(即FK),您需要提供原始FK值,以便ef可以删除原始PK-FK对并插入新的PK-FK对。正如您所知,使用FK关联时,这种情况会发生变化。主要是因为FK现在在结构上是实体的一部分,这意味着您不再需要原始值来更新它。