如何从 Fluent Api 检索实体配置

人气:671 发布:2022-10-16 标签: .net c# entity-framework entity-framework-6 ef-fluent-api

问题描述

使用 Entity-Framework 6 我可以像这样通过 Fluent Api 设置配置:

Using Entity-Framework 6 I'm able to set up the configuration through Fluent Api like this:

public class ApplicationUserConfiguration : EntityTypeConfiguration<ApplicationUser>
{
    public ApplicationUserConfiguration()
    {
        this.HasKey(d => d.Id);
        this.Ignore(d => d.UserId);
    }
}

来源来自这个问题

使用属性方法我可以通过反射知道属性角色是什么,但我想知道如何使用 Fluent Api 方法检索这些配置,例如 Key?

Using the attribute approach I'm able to know what's the property roles by reflection, but I wonder how can I retrieve these configurations, like Key for example, with Fluent Api approach?

EntityTypeConfiguration<> 类没有公共属性.

是否有可能以某种方式获得 KeyForeignKey?

Is that possible to get the Key and ForeignKey somehow?

推荐答案

有一个MetadataWorkspace 类,它提供 API 来检索有关存储、模型、实体框架的 CLR 类型和映射.

There is a MetadataWorkspace class which provides API to retrieve metadata about storage, model, CLR types and mappings for Entity Framework.

表示 ADO.NET 元数据运行时服务组件,它支持从各种来源检索元数据.

Represents the ADO.NET metadata runtime service component that provides support for retrieving metadata from various sources.

拥有一个 DbContext 的实例,您可以使用以下代码找到它的 MetadataWorkspace:

Having an instance of DbContext, you can find its MetadataWorkspace using following code:

var metadataWorkspace = ((IObjectContextAdapter)dbContext).ObjectContext.MetadataWorkspace;

然后您可以获取包含不同类型模型的项目集合,包括对象模型、概念模型、存储(数据库)模型以及概念模型和存储模型之间的映射模型.

Then you can get item collections which contain different types of models including object model, the conceptual model, the storage (database) model, and the mapping model between the conceptual and storage models.

以下扩展方法返回 EntityType 对于给定的 clr 类型:

The following extension methods returns EntityType for given clr type:

using System;
using System.Data.Entity;
using System.Data.Entity.Core.Metadata.Edm;
using System.Data.Entity.Infrastructure;
using System.Linq;

public static class DbContextExtensions
{
    public static EntityType GetEntityMetadata<TEntity>(this DbContext dbContext)
    {
        if (dbContext == null)
            throw new ArgumentNullException(nameof(dbContext));

        var metadataWorkspace = ((IObjectContextAdapter)dbContext)
            .ObjectContext.MetadataWorkspace;
        var itemCollection = ((ObjectItemCollection)metadataWorkspace
            .GetItemCollection(DataSpace.OSpace));
        var entityType = metadataWorkspace.GetItems<EntityType>(DataSpace.OSpace)
            .Where(e => itemCollection.GetClrType(e) == typeof(TEntity)).FirstOrDefault();

        if (entityType == null)
            throw new Exception($"No entity mapped to CLR type '{typeof(TEntity)}'.");

        return entityType;
    }
}

然后你可以使用 EntityType 来提取关于模型的更多信息,例如你可以找到一个关键属性列表:

Then you can use EntityType to extract more information about the model, for example you can find a list of key properties:

var keys = dbcontext.GetEntityMetadata<Category>().KeyProperties.Select(x=>x.Name).ToList();

725