EntityRef-object not refreshed after LINQ-update?
If have 2 entity classes, ProductEntity and CategoryEntity:
[Table(开发者_如何学GoName = "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
Using LINQ I execute a Select as follows (details omitted):
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();
Obtained value for the 1st record in the list is
productList[0].ProductName "TestProduct"
productList[0].CategoryID 1
productList[0].Category.CategoryID "1"
productList[0].Category.CategoryName "Beverages"
Then I update CategoryID to 8 OK, so far so good
I reexceute the select as above. Obtained value for the 1st record is now
productList[0].ProductName "TestProduct"
productList[0].CategoryID 8 OK
productList[0].Category.CategoryID "1" ??
productList[0].Category.CategoryName "Beverages" ??
The CategoryID-property of ProductEntity has been updated but the Category-property not, in other words the EntityRef object has not been 'rebuild' ???
Why not? How can i make this work?
thank you Chris
精彩评论