【问题标题】:Web API not adding Controller, Sqlite error?Web API 未添加控制器,Sqlite 错误?
【发布时间】:2020-02-15 13:54:22
【问题描述】:

我有一个 .Net Core 2.2 应用程序,我进行了迁移并成功更新了数据库,我想使用 Entity Framework Core 添加一个带有动作的 API 控制器,但它不会成功搭建。在此之前我做了一个类似的应用程序,它生成的控制器很好,我不确定问题是什么,我尝试自己解决它,但找不到解决方案。

在包控制台我得到这个信息:

无法识别的选项'--useSqlite' 在 Microsoft.Extensions.CommandLineUtils.CommandLineApplication.HandleUnexpectedArg(CommandLineApplication 命令,字符串 [] args,Int32 索引,字符串 argTypeName) 在 Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args) 在 Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.Execute(String[] args) 在 Microsoft.VisualStudio.Web.CodeGeneration.CodeGenCommand.Execute(String[] args)

我已尝试卸载 sqlite NuGetpackage,但仍然出现错误,我尝试重新构建应用程序并重新启动 Visual Code,但它仍然无法正常工作。

这是我的文件

Startup.cs

    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            services.AddDbContext<LoanContext>(options =>
            options.UseSqlite(Configuration.GetConnectionString("DevConnection")));
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseMvc();
        }
    }
}

appsetting.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*",
  "connectionStrings": {
    "DevConnection": "server=(local)\\sqlite;Database=TestDB;User Id=sa;Password=LovelyWorld1;"
  }
}

贷款.cs

 public class Loan
    {
        [Key]

        public int Details { get; set; }
        [Column(TypeName = "nvarchar(250)")]
        [Required]

        public string LoanID { get; set; }
        [Column(TypeName = "nvarchar(10)")]

        public string BorrowerName { get; set; }
        [Column(TypeName = "nvarchar(100)")]

        public string FundingAmount { get; set; }
        [Column(TypeName = "nvarchar(100)")]

        public string RepaymentAmount { get; set; }

    }
}

【问题讨论】:

  • 您似乎在使用 SQLite,但连接字符串适用于 SQL Server 等数据库服务器。你有这个项目在 GitHub 上还是我可以查到的地方?

标签: c# visual-studio api .net-core controller


【解决方案1】:

我认为主要问题在于您的贷款模型属性。 他们不在正确的地方。例如,我认为 key 属性应该在 LoadID 之上。另外, loadID 是一个字符串,我认为它应该是 int 。我改变了这样的模型:

 public class Loan
    {
        [Key] 
        public int LoanID { get; set; }

        [Column(TypeName = "nvarchar(250)"), Required]
        public string Details { get; set; }

        [Column(TypeName = "nvarchar(10)")] 
        public string BorrowerName { get; set; }

        [Column(TypeName = "nvarchar(100)")] 
        public string FundingAmount { get; set; }

        [Column(TypeName = "nvarchar(100)")] 
        public string RepaymentAmount { get; set; }
    }

然后我删除了您的初始化迁移并添加了一个新迁移。我用 SQL Server 和 SQLite 测试了迁移,结果很好。 请确保在使用 slite 时应该像这样设置连接字符串

“数据源=loanDb.db”

注意:“FundingAmount”和“RepaymentAmount”字段最好使用小数而不是字符串。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 2017-03-02
    • 2020-03-07
    • 1970-01-01
    • 2014-02-20
    • 2015-01-03
    • 2018-04-23
    相关资源
    最近更新 更多