如何使用反射 .dll 从 Entity Framework Core 2 数据库脚手架中找到主键?
我们需要根据实体名称找到主键成员,并将其作为字符串返回。
在 Sql Server 数据库上进行了反向脚手架。
modelBuilder.Entity<Product>(entity =>
{
entity.HasKey(e => e.ProductInfoId)
.HasName("PK_ProductInfoId");
尝试解决方案:
试图编写下一行代码,给出字符串实体名称:“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"))
其他资源:
出于代码生成工具的考虑,更倾向于使用反射而非实例化上下文来完成此操作,将为10个数据库项目执行。
DbContext
派生类实例吗?你所要求的信息是由EF Core元数据API提供的,需要DbContext
实例。就像这里https://dev59.com/GrPma4cB1Zd3GeqPrYDy#55880724 - Ivan Stoev