如果有 2 个实体类 ProductEntity 和 CategoryEntity:
[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 我执行 Select 如下(省略详细信息):
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 更新为 8 OK,到目前为止一切顺利
我重新执行上面的选择。第一条记录的获得值现在是
productList[0].ProductName "TestProduct"
productList[0].CategoryID 8 OK
productList[0].Category.CategoryID "1" ??
productList[0].Category.CategoryName "Beverages" ??
ProductEntity 的 CategoryID-property 已更新,但 Category-property 未更新,换句话说,EntityRef 对象尚未“重建”???
为什么不?我怎样才能使这项工作?
谢谢克里斯