【问题标题】:How to read Custom Attributes using reflection set by Fluent API in EF 4.1如何使用 EF 4.1 中 Fluent API 设置的反射读取自定义属性
【发布时间】:2011-08-18 07:52:56
【问题描述】:

当我使用数据注释时,我已经设法读取自定义属性。 像下面的代码。

Object[] test = propertyInfo.GetCustomAttributes(typeof(KeyAttribute), true);

但是当我改用 Fluent API 时。我无法再读取该属性。 有什么想法吗?

【问题讨论】:

    标签: entity-framework-4.1 fluent-interface


    【解决方案1】:

    Fluent API 不设置属性。 Fluent API 和 Attributes 告诉 EF 如何构建模型。

    这是实现同一目标的两种不同方式。也就是建立模型。

    编辑

    如果您需要检索主键等元数据,您需要访问MetadataWorkspace

    这个article有详细信息。

    【讨论】:

    • 我真的需要用 Fluent API 设置属性。无论如何,我可以从反射中回读吗?
    • 我设法通过以下代码获取了 MetadataWorkSpace。 ObjectContext objContext = ((IObjectContextAdapter)_MitecsDBContext).ObjectContext;但我想知道如何设置自定义属性以使以下代码有效。 Object[] test = propertyInfo.GetCustomAttributes(typeof(KeyAttribute), true);
    • @Soe 它在我链接的文章中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-14
    • 2023-03-07
    相关资源
    最近更新 更多