简体   繁体   English

Lamar脚手架实体框架控制器

[英]Scaffolding Entity Framework Controllers with Lamar

I have Lamar set up in my .NET Core 2 project: 我在.NET Core 2项目中设置了Lamar:

    public class Program
    {
        public static void Main(string[] args)
        {
            IWebHost webhost = CreateWebHostBuilder(args).Build();
            //((Container)webhost.Services).GetInstance<IStart>().Run();
            webhost.Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseLamar()
                .UseStartup<Startup>();
    }

...

    public class Startup
    {
    ...
        public void ConfigureContainer(ServiceRegistry services)
        {
            services.Configure<Configuration.Auth>("auth", Configuration);
            ...
            services.Scan(s =>
            {
                s.TheCallingAssembly();
                s.WithDefaultConventions();
            });
            services.AddCors();
            services.AddMvc()
                .AddJsonOptions(o =>
                {
                    o.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
                    o.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
                })
                .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
            services.AddDbContext<Context>(options => options.UseSqlServer(Configuration.GetConnectionString("defaultConnection")));
        }
    }

However, when attempting to use Scaffold API Controller with actions, using Entity Framework I run into the following error: 但是,当尝试将Scaffold API Controller与动作配合使用时,使用Entity Framework我遇到以下错误:

There was an error running the selected code generator: 'No parameterless constructor defined for this object.' 运行所选代码生成器时发生错误:“未为此对象定义无参数构造函数。”

Looking up this https://docs.microsoft.com/en-us/aspnet/core/migration/1x-to-2x/?view=aspnetcore-2.2#update-main-method-in-programcs suggested that this can show up in ASP.NET Core 2 projects that attempt to still use the .NET 1.x structure. 查找此https://docs.microsoft.com/zh-cn/aspnet/core/migration/1x-to-2x/?view=aspnetcore-2.2#update-main-method-in-programcs建议可以显示试图仍然使用.NET 1.x结构的ASP.NET Core 2项目。

I found a hacky work-around that I'll post below, which suggests that the scaffolding code generation may have an issue with Lamar. 我在下面找到了一个骇人的解决方法,这表明脚手架代码生成可能与Lamar有关。 However, is there a better solution? 但是,有没有更好的解决方案? Can you set up Lamar to be able to handle Entity Framework Code generation? 您可以设置Lamar使其能够处理实体框架代码生成吗?

Considering EF was failing in the generate code section, I wondered if perhaps the issue was not the parameterless constructor (I'm pretty sure that whatever unnamed object it was referring to actually has one) but the issue with how the WebHost gets built when using Lamar. 考虑到EF在“生成代码”部分中失败,我想知道问题是否可能不是无参数构造函数(我很确定它所指的任何未命名对象实际上都有一个),而是有关使用时如何构建WebHost的问题拉马尔。

The note in the Lamar documentation on integrating with ASP.NET Core states Lamar文档中有关与ASP.NET Core集成的说明

Note! 注意! The Startup.ConfigureServices(ServiceRegistry) convention does not work as of ASP.Net Core 2.1. 从ASP.Net Core 2.1开始, Startup.ConfigureServices(ServiceRegistry)约定不起作用。 Use ConfigureContainer(ServiceRegistry) instead. 请改用ConfigureContainer(ServiceRegistry)

I was using that Lamar function in my Startup; 我在启动中使用了Lamar函数; however, if I changed it back to ConfigureContainer(IServiceCollection services) (and commented out the Lamar-specific functions, such as Scan), I found that I was able to scaffold the EF controller again. 但是,如果我将其改回ConfigureContainer(IServiceCollection services) (并注释掉了Lamar特定的功能,例如“扫描”),我发现我可以再次搭建EF控制器。

So, at the moment, my workaround is to comment out Lamar before scaffolding, and then uncomment it back once I'm done. 因此,目前,我的解决方法是在脚手架之前注释掉Lamar,然后在完成后取消注释。 I suspect there may be a better solution though... 我怀疑可能会有更好的解决方案...

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

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