LINQ更新后EntityRef-object没有刷新?

时间:2011-05-25 10:06:18

标签: entityreference

如果有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 好的,到目前为止一切顺利

我重新如上所述选择。 现在获得第一条记录的值

    productList[0].ProductName      "TestProduct"   

    productList[0].CategoryID       8                        OK

    productList[0].Category.CategoryID  "1"                    ??

    productList[0].Category.CategoryName    "Beverages"            ??

ProductEntity的CategoryID属性已更新,但Category-property没有,换句话说,EntityRef对象尚未'重建'???

为什么不呢?我怎样才能做到这一点?

谢谢你 克里斯

0 个答案:

没有答案