简体   繁体   English

Net Core:查找 Entity Framework Core 的主键和反射

[英]Net Core: Find Primary Key of Entity Framework Core and Reflection

How do I find Primary Key from an Entity Framework Core 2 Database Scaffold using Reflection .dll?如何使用 Reflection .dll 从 Entity Framework Core 2 Database Scaffold 中找到主键?

We need to find Primary Key Member given an EntityName, and return Primary Key Member as string.我们需要找到给定 EntityName 的主键成员,并将主键成员作为字符串返回。 Conducted Reverse Scaffolding on Sql Server database .对 Sql Server 数据库进行反向脚手架。

modelBuilder.Entity<Product>(entity =>
{
    entity.HasKey(e => e.ProductInfoId)
     .HasName("PK_ProductInfoId");

Attempting Solution:尝试解决方案:

Trying to write next line code, given string Entity Name: "Product".尝试编写下一行代码,给定字符串实体名称:“产品”。

var assembly = Assembly.LoadFrom(@"C:\OnlineShoppingStore\bin\Debug\netcoreapp2.2\OnlineShoppingStore.dll");

assembly.GetTypes().Where(d=>d.Name = "OnlineStoreDbContext")

etc GetProperties().Where(e=>e.Name == "Product"))

Other Resource:其他资源:

Prefer to do this with Reflection, rather than instantiating context since this is for code generation tool, will conduct for 10 db projects.更喜欢用反射来做这个,而不是实例化上下文,因为这是代码生成工具,将执行 10 个数据库项目。

Generic Repository in C# Using Entity Framework 使用实体框架的 C# 中的通用存储库

Expanding on suggestions from comments, you can instantiate the context in your code and invoke all EF model inspection APIs as outlined in Ivan's answer like so:扩展来自评论的建议,您可以在代码中实例化上下文并调用所有 EF 模型检查 API,如Ivan 的回答中所述,如下所示:

var assembly = Assembly.LoadFrom(@"C:\OnlineShoppingStore\bin\Debug\netcoreapp2.2\OnlineShoppingStore.dll");
var contextType = assembly.GetTypes().First(d => d.Name == "OnlineStoreDbContext");
var ctx = Activator.CreateInstance(contextType) as DbContext; // instantiate your context. this will effectively build your model, so you must have all required EF references in your project
var p = ctx.Model.FindEntityType(assembly.GetTypes().First(d => d.Name == "Product")); // get the type from loaded assembly
//var p = ctx.Model.FindEntityType("OnlineStoreDbContext.Product"); // querying model by type name also works, but you'd need to correctly qualify your type names
var pk = p.FindPrimaryKey().Properties.First().Name; // your PK property name as built by EF model

UPD : Forget thought experiments, it clearly is confusing. UPD忘记思想实验,这显然令人困惑。 The method above does not require you to reference your other projects and requires no prior knowledge of types except for string names (which you have as it seems).上面的方法不需要你引用你的其他项目,并且不需要除了字符串名称(你看起来有)之外的类型的先验知识。 When you instantiate your db context, the base class constructor in EF will process all your custom overrides and will return you a fully built model (ie all properties will be accounted for).当您实例化您的 db 上下文时,EF 中的基类构造函数将处理您所有的自定义覆盖,并将返回一个完全构建的模型(即所有属性都将被考虑在内)。 Again, no reference will be required as long as you only use EF metadata API (which is available on base DBContext class).同样,只要您只使用 EF 元数据 API(可在基本DBContext类上使用),就不需要引用。 Hopefully that clarifies.希望这能澄清。

UPD2 : following up on my IL Emit idea in comments. UPD2 :在评论中跟进我的IL Emit想法。 See the implementation and a bit more background in my blog .我的博客中查看实现和更多背景信息。 This enabled me to eliminate the exception (still have to have at least one DB Provider available though).这使我能够消除异常(尽管仍然必须至少有一个可用的数据库提供程序)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM