如何从 Fluent Api 检索实体配置

编程入门 行业动态 更新时间:2024-10-21 07:51:30
本文介绍了如何从 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<> 类没有公共属性.

是否有可能以某种方式获得 Key 和 ForeignKey?

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();

更多推荐

如何从 Fluent Api 检索实体配置

本文发布于:2023-11-17 04:46:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1608881.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实体   Fluent   Api

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!