ProductEntityとCategoryEntityの2つのエンティティクラスがある場合:
[Table(Name = "Products")]
public class ProductEntity
{
// Data Members
private int _productID;
private string _productName;
private int _categoryID;
private EntityRef<CategoryEntity> _category;
// Properties
[Column(DbType = "int", IsPrimaryKey = true, IsDbGenerated = true)]
public int ProductID
{
get { return _productID; }
set { _productID = value; }
}
[Column(DbType = "nvarchar(40)")]
public string ProductName
{
get { return _productName; }
set { _productName = value; }
}
[Column(DbType = "int")]
public int CategoryID
{
get { return _categoryID; }
set { _categoryID = value; }
}
[Association(Storage = "_category",
ThisKey = "CategoryID",
OtherKey = "CategoryID")]
public CategoryEntity Category
{
get { return _category.Entity; }
set { _category.Entity = value; }
}
} // class ProductEntity
[Table(Name = "Categories")]
public class CategoryEntity
{
// Data Members
private int _categoryID;
private string _categoryName;
// Properties
[Column(DbType = "int", IsPrimaryKey = true)]
public int CategoryID
{
get { return _categoryID; }
set { _categoryID = value; }
}
[Column(DbType = "nvarchar(40)")]
public string CategoryName
{
get { return _categoryName; }
set { _categoryName = value; }
}
} // class CategoryEntity
LINQを使用して、次のように選択を実行します(詳細は省略)。
DataContext _northwindCtx = new DataContext(connectionString);
DataLoadOptions loadOptions = new DataLoadOptions();
loadOptions.LoadWith<ProductEntity>
(ProductEntity => ProductEntity.Category);
_northwindCtx.LoadOptions = loadOptions;
Table<ProductEntity> productsList =
_northwindCtx.GetTable<ProductEntity>();
var productsQuery =
from ProductEntity product in productsList
where product.ProductName.StartsWith(productname)
select product;
List<ProductEntity> productList = productsQuery.ToList();
リストの最初のレコードの取得値は次のとおりです。
productList[0].ProductName "TestProduct"
productList[0].CategoryID 1
productList[0].Category.CategoryID "1"
productList[0].Category.CategoryName "Beverages"
次に、CategoryIDを8OKに更新します。
上記のように選択を再実行します。1番目のレコードの取得値は現在
productList[0].ProductName "TestProduct"
productList[0].CategoryID 8 OK
productList[0].Category.CategoryID "1" ??
productList[0].Category.CategoryName "Beverages" ??
ProductEntityのCategoryIDプロパティは更新されましたが、Categoryプロパティは更新されていません。つまり、EntityRefオブジェクトは「再構築」されていません。
なぜだめですか?どうすればこれを機能させることができますか?
ありがとうクリス