0

如果您没有使用 EntitySpaces ("ES") ORM 的经验或当前未使用此问题,则此问题不适合您。

我有一个 10 年前的应用程序,4 年后现在需要我注意。我的应用程序使用了一个现已失效的 ORM,称为 EntitySpaces,我希望如果您正在阅读这篇文章,您有经验或者可能还在使用它!目前不能切换到另一个 ORM,所以我需要找到一种方法来完成这项工作。

从我上次积极开发我的应用程序到现在(ES 版本 2012-09-30)之间,EntitySpaces(“ES”)在底层 ADO.net 后端发生了重大变化。我正在寻求帮助的场景是实体集合仅加载了列的子集:

_products = new ProductCollection();
_products.Query.SelectAllExcept(_products.Query.ImageData);
_products.LoadAll();

然后我会覆盖初始选择中未加载的属性,以便我可以将它们延迟加载到访问器中。这是一个这样的延迟加载属性的示例,它曾经完美地工作。

public override byte[] ImageData
{
    get
    {
        bool rowIsDirty = base.es.RowState != DataRowState.Unchanged;

        //  Check if we have loaded the blob data
        if(base.Row.Table != null && base.Row.Table.Columns.Contains(ProductMetadata.ColumnNames.ImageData) == false)
        {
            //  add the column before we can save data to the entity
            this.Row.Table.Columns.Add(ProductMetadata.ColumnNames.ImageData, typeof(byte[]));
        }

        if(base.Row[ProductMetadata.ColumnNames.ImageData] is System.DBNull)
        {
            //  Need to load the data
            Product product = new Product();
            product.Query.Select(product.Query.ImageData).Where(product.Query.ProductID == base.ProductID);
            if(product.Query.Load())
            {
                if (product.Row[ProductMetadata.ColumnNames.ImageData] is System.DBNull == false)
                {
                    base.ImageData = product.ImageData;

                    if (rowIsDirty == false)
                    {
                        base.AcceptChanges();
                    }
                }
            }
        }

        return base.ImageData;
    }
    set
    {
        base.ImageData = value;
    }
}

有趣的部分是我将列添加到基础 DataTable DataColumn 集合的位置:

this.Row.Table.Columns.Add(ProductMetadata.ColumnNames.ImageData, typeof(byte[]));

当我更新到 ES 的当前(和开源)版本(版本 2012-09-30)时,我不得不从该访问器中注释掉所有与 ADO.net 相关的内容。这意味着“ImageData”列未正确配置,当我更改它的数据并尝试保存实体时,我收到以下错误:

列 'ImageData' 不属于 table 。

我花了几天时间查看 ES 源代码并进行试验,似乎他们不再使用 DataTable 来支持实体,而是使用“esSmartDictionary”。

我的问题是:是否有一种已知的、受支持的方法来完成在新版本的 ES 中使用的相同延迟加载行为?通过告诉 ORM 将其添加到实体后备存储中,我可以在哪里更新未包含在初始选择中的属性(即列)?

4

1 回答 1

0

在分析了 ES 如何构造用于更新的 DataTable 之后,很明显未包含在初始选择(即加载)操作中的列需要添加到 esEntityCollectionBase.SelectedColumns 字典中。我添加了以下方法来处理这个问题。

/// <summary>
/// Appends the specified column to the SelectedColumns dictionary. The selected columns collection is 
/// important as it serves as the basis for DataTable creation when updating an entity collection. If you've 
/// lazy loaded a column (i.e. it wasn't included in the initial select) it will not be automatically 
/// included in the selected columns collection. If you want to update the collection including the lazy 
/// loaded column you need to use this method to add the column to the Select Columns list.
/// </summary>
/// <param name="columnName">The lazy loaded column name. Note: Use the {yourentityname}Metadata.ColumnNames 
/// class to access the column names.</param>
public void AddLazyLoadedColumn(string columnName)
{
    if(this.selectedColumns == null)
    {
        throw new Exception(
            "You can only append a lazy-loaded Column to a partially selected entity collection");
    }

    if (this.selectedColumns.ContainsKey(columnName))
    {
        return;
    }
    else
    {
        //  Using the count because I can't determine what the value is supposed to be or how it's used. From 
        //  I can tell it's just the number of the column as it was selected: if 8 colums were selected the 
        //  value would be 1 through 8 - ??
        int columnValue = selectedColumns.Count;
        this.selectedColumns.Add(columnName, columnValue);
    }
}

你会像这样使用这个方法:

public override System.Byte[] ImageData
{
    get
    {
        var collection = this.GetCollection();
        if(collection != null)
        {
            collection.AddLazyLoadedColumn(ProductMetadata.ColumnNames.ImageData);
        }
...

可惜没有人对开源 EntitySpaces 感兴趣。如果我认为它有未来,我会很乐意为它工作,但事实并非如此。:(

我仍然对其他用户的任何其他方法或见解感兴趣。

于 2016-04-27T21:35:31.320 回答