私は.NET 3.5 SP1を使用しています。テーブル「category_table」に基づいてエンティティ「Category」を作成し、テーブル「App_User_table」に基づいて「MyUser」を作成しました。
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 {...}
...........
}
ASP.NET MVCアプリケーションでAppUserエンティティを追加および更新したいです。
追加する場合
//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から選択したCategoryIdを更新します
user.category_tableReference.EntityKey = new System.Data.EntityKey("MyContext.CategorySet", "CategoryId", categoryId);
updateメソッドはデータベース内のCategoryIdを更新しません。
問題を解決するための最善の方法は何ですか。
ありがとうございました。
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を得ることができる必要があるのを見ることができるように、私はフォーム上の隠されたフィールドをお勧めします。
これはあなたが必要とするすべてをするでしょう。 Stub Entityのコツに関する詳細は、 ヒント26 - Stub Entitiesを使用したデータベースクエリを回避する方法をご覧ください。
お役に立てれば
アレックス
更新:Craig StuntzとAlex Jamesのコメントを見てください - 私は正解です - あなたが要求したことを達成するために単に "EntityKey"を更新することは可能であるように見えます。ここの私の投稿はもう当てはまりません
バージョン1では、Entity Frameworkは挿入されている外部キー値だけでは動作できません - "Category"オブジェクトのインスタンスを "AppUser"のCateogryナビゲーションプロパティに追加する必要があります。
//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);
今年の後半(2009年)に.NET 4.0に同梱されるEF v4では、2つのエンティティ間の関連付けを行うために外部キーの値だけを設定することができます。
これについてのより多くの情報をここに見なさい:
しかし今のところ、あなたは完全なオブジェクトを設定する必要があります - ただの参照はしません。
マーク