首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在EntitySpaces中添加延迟加载的列?

如何在EntitySpaces中添加延迟加载的列?
EN

Stack Overflow用户
提问于 2016-04-27 08:18:29
回答 1查看 250关注 0票数 0

如果您没有使用EntitySpaces ("ES") ORM的经验,或者目前没有使用EntitySpaces(“ES”)ORM,那么这个问题并不适合您。

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

从我上一次积极处理我的应用程序到现在(ES版本为2012-09-30),EntitySpaces ("ES")在底层的ADO.net后端经历了重大变化。我正在寻求帮助的场景是,当实体集合只装载列的一个子集时:

代码语言:javascript
复制
_products = new ProductCollection();
_products.Query.SelectAllExcept(_products.Query.ImageData);
_products.LoadAll();

然后,我重写了初始select中没有加载的属性,以便在访问器中加载它们。下面是一个这样的延迟加载属性的例子,它曾经完美地工作过。

代码语言:javascript
复制
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集合中:

代码语言:javascript
复制
this.Row.Table.Columns.Add(ProductMetadata.ColumnNames.ImageData, typeof(byte[]));

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

列'ImageData‘不属于表。

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

我的问题是:是否有一种已知的、受支持的方法来完成以前在新版本的ES中工作的懒惰加载行为?通过告诉ORM将其添加到实体后备存储中,可以更新未包含在初始select中的属性(即列)。

EN

回答 1

Stack Overflow用户

发布于 2016-04-27 21:35:31

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

代码语言:javascript
复制
/// <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);
    }
}

您可以使用这样的方法:

代码语言:javascript
复制
public override System.Byte[] ImageData
{
    get
    {
        var collection = this.GetCollection();
        if(collection != null)
        {
            collection.AddLazyLoadedColumn(ProductMetadata.ColumnNames.ImageData);
        }
...

遗憾的是,没有人对开源EntitySpaces感兴趣。如果我认为它有未来的话,我很乐意为它工作,但它并不是这样的。:(

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36884137

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档