【问题标题】:EF5: Can't Get StoreGeneratedPattern Property From ModelEF5:无法从模型中获取 StoreGeneratedPattern 属性
【发布时间】:2013-08-08 03:07:31
【问题描述】:

我在至少 2 个位置(herehere)看到了用于读取实体框架 EdmProperty 的 StoreGeneratedPattern 属性(计算、身份或无)的示例代码,但它不适用于我的模型。我的上下文是一个ObjectContext,版本是EF5;也许这段代码与 EF5 冲突?当我在模型中查看此属性/列的属性时,它会显示身份。

这是我的代码:

using ( var context = new MyApplicationEntities() )
{
    var entityType = ( (EntityConnection)context.Connection )
        .GetMetadataWorkspace() // can't call context.MetadataWorkspace - storage model will not be present
        .GetType( "MyEntityTypeWithIdentityColumn", "MyApplicationModel.Store", DataSpace.SSpace ) as EntityType;

    EdmMember identityColumn = entityType.Members["MyIdentityColumn"];

    Facet item;
    // All I get here for Facets is Nullable & DefaultValue
    if ( identityColumn.TypeUsage.Facets.TryGetValue( "StoreGeneratedPattern", false, out item ) )
    {
        var value = ( (StoreGeneratedPattern)item.Value ) == StoreGeneratedPattern.Identity;
    }
}

【问题讨论】:

  • 你能展示你的模型吗?
  • 显示相关列的图像?

标签: entity-framework entity-framework-5


【解决方案1】:

Pawel 的问题让我检查了 edmx 并找到了答案。我在我的问题中说,我可以查看模型(UI)并看到有问题的列是一个标识列。事实上,事实并非如此。在任何情况下,我都希望 StoreGeneratedPattern 属性存在,因为枚举包含所有可能的值(Identity、Computed、None)。

我的实际目标是检测主键列/属性不是身份的所有实体。

通过后续测试发现StoreGeneratedPattern 属性只存在于identity & computed;如果该列不是存储生成的,则它不存在(StoreGeneratedPattern.None 永远不会被引用)。因此,只要修改上面的示例代码以处理该属性的缺失(这对我来说似乎很奇怪),就可以了。

【讨论】:

    猜你喜欢
    • 2023-04-06
    • 1970-01-01
    • 2014-12-10
    • 1970-01-01
    • 2013-06-06
    • 1970-01-01
    • 2020-09-06
    • 1970-01-01
    相关资源
    最近更新 更多