如何将引用程序集用于控制器脚手架
How to use referenced assembly for controller scaffolding
我有一个 class 库,其中包含一些模型 classes 和 DbContext class(所有这些 classes 都是 public)。此 class 库被 MVC-5 应用程序引用。
是否可以使用引用的 class 库中的模型 classes 来构建该 MVC-5 应用程序中的控制器?
当我使用 Controllers - Add - Controller - MVC Controllers with views 时,使用 Entity Framework 然后在对话框中 Model [= 的两个组合框28=] 和 数据上下文 class 不包含任何项目。当我从引用的 class 库中填写 class 的完全限定名称时,Add 按钮仍然被禁用。我做错了什么?
将引用程序集编译为 dll 文件的脚手架根本不起作用。有必要添加对包含模型 类 的项目的引用。然后它按预期工作。 这是错误还是功能?
通过一些调整,您可以使用脚手架引用外部 dll(例如 Entity Framework 项目)。
为此,您需要创建一个 class 来继承您的 EF table class。为了工作,您需要在 class 之上使用具有正确模式和 table 名称的 [Table] 属性,否则脚手架将创建一个新的 table。
还要确保使用 "new" 关键字并重载 id。您将需要使用 [Key] 属性(如果尚未在原始 dll 中的 EF table 中定义)。
最后创建一个新的 dbcontext 并确保在 web.config 中使用连接字符串 ID。
这应该允许您在 Web 项目中引用 table 和上下文。
这是我的代码(非常简单),希望对您有所帮助。
namespace ConsoleAdmin.Models
{
[Table("ntf.tblNotification_ntf")]
public class Notification : tblNotification_ntf
{
[Key]
public new int notificationId { get; set; }
}
public class NotificationDbContext : DbContext
{
public NotificationDbContext(): base("name=bd_Soquij_logEntities") { }
public DbSet<Notification> Notifications { get; set; }
}
}
我有一个 class 库,其中包含一些模型 classes 和 DbContext class(所有这些 classes 都是 public)。此 class 库被 MVC-5 应用程序引用。
是否可以使用引用的 class 库中的模型 classes 来构建该 MVC-5 应用程序中的控制器?
当我使用 Controllers - Add - Controller - MVC Controllers with views 时,使用 Entity Framework 然后在对话框中 Model [= 的两个组合框28=] 和 数据上下文 class 不包含任何项目。当我从引用的 class 库中填写 class 的完全限定名称时,Add 按钮仍然被禁用。我做错了什么?
将引用程序集编译为 dll 文件的脚手架根本不起作用。有必要添加对包含模型 类 的项目的引用。然后它按预期工作。 这是错误还是功能?
通过一些调整,您可以使用脚手架引用外部 dll(例如 Entity Framework 项目)。
为此,您需要创建一个 class 来继承您的 EF table class。为了工作,您需要在 class 之上使用具有正确模式和 table 名称的 [Table] 属性,否则脚手架将创建一个新的 table。
还要确保使用 "new" 关键字并重载 id。您将需要使用 [Key] 属性(如果尚未在原始 dll 中的 EF table 中定义)。
最后创建一个新的 dbcontext 并确保在 web.config 中使用连接字符串 ID。
这应该允许您在 Web 项目中引用 table 和上下文。
这是我的代码(非常简单),希望对您有所帮助。
namespace ConsoleAdmin.Models
{
[Table("ntf.tblNotification_ntf")]
public class Notification : tblNotification_ntf
{
[Key]
public new int notificationId { get; set; }
}
public class NotificationDbContext : DbContext
{
public NotificationDbContext(): base("name=bd_Soquij_logEntities") { }
public DbSet<Notification> Notifications { get; set; }
}
}