[英]Sending and receiving different models over json with webapi and entity framework
[英]Distribute entity framework models over different assemblies
目前我正在嘗試使用實體框架6.3和一種分布式模型。 我的問題是,我想在不同的程序集中分離模型(例如核心應用程序和插件)。
現在我正在尋找一種方法,我的應用程序可以通過反射或類似的東西獲得所有模型,並在啟動之前在ef db上下文中注冊所有模型。
這可能與實體框架6有關嗎?
謝謝。
您可以在反射中搜索每個裝配中的模型。
迭代AppDomain的程序集並搜索屬性,接口或基類,請參閱下面的示例。
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
var entityMethod = typeof(DbModelBuilder).GetMethod("Entity");
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
var entityTypes = assembly
.GetTypes()
.Where(t =>
t.GetCustomAttributes(typeof(PersistentAttribute), inherit: true)
.Any());
foreach (var type in entityTypes)
{
entityMethod.MakeGenericMethod(type)
.Invoke(modelBuilder, new object[] { });
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.