.Net错误:System.ArgumentException:'不支持的关键字:'trustedconnection'. [英] .Net error: System.ArgumentException: 'Keyword not supported: 'trustedconnection'.'

查看:28
本文介绍了.Net错误:System.ArgumentException:'不支持的关键字:'trustedconnection'.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个尝试使用.Net编写的应用程序.我有一个SeedData.cs类,我试图用它来填充数据库,但遇到一些连接问题,并且继续出现错误:System.ArgumentException:'不支持的关键字:'trustedconnection'.行:

I have an application that I am trying to write with .Net. I have a SeedData.cs class that I am trying to use to populate my database, but I am experiencing some connection issues and I keep on getting the error: System.ArgumentException: 'Keyword not supported: 'trustedconnection'.'for the following line:


    if (!context.Products.Any())

我认为这可能是由于我的数据库连接造成的,但是无论如何,这是我的代码:

I think this might be due to my database connection, but anyhow this is my code:

//SeedData.cs

//SeedData.cs


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.Extensions.DependencyInjection;
    
    namespace SportsStore.Models
    {
        public static class SeedData
        {
            public static void EnsurePopulated(IApplicationBuilder app)
            {
                ApplicationDbContext context = app.ApplicationServices.GetRequiredService<ApplicationDbContext>();
                if (!context.Products.Any())
                {
                    context.Products.AddRange(
                        new Product
                        {
                            Name = "Kayak",
                            Description = "A boat for one person",
                            Category = "Watersports",
                            Price = 275
                        },
                        new Product
                        {
                            Name = "Lifejacket",
                            Description = "Protective and fashionable",
                            Category = "Watersports", Price = 48.95m
                        },
                        new Product
                        {
                            Name = "Soccer Ball",
                            Description = "FIFA-approved size and weight",
                            Category = "Soccer", Price = 19.50m
                        },
                        new Product
                        {
                            Name = "Corner Flags",
                            Description = "Give your playing field a professional touch",
                            Category = "Soccer",
                            Price = 34.95m
                        },
                        new Product
                        {
                            Name = "Stadium",
                            Description = "Flat-packed 35,000-seat stadium",
                            Category = "Soccer",
                            Price = 79500
                        },
                        new Product
                        {
                            Name = "Thinking Cap",
                            Description = "Improve brain efficiency by 75%",
                            Category = "Chess",
                            Price = 16
                        },
                        new Product
                        {
                            Name = "Unsteady Chair",
                            Description = "Secretly give your opponent a disadvantage",
                            Category = "Chess",
                            Price = 75
                        },
                        new Product
                        {
                            Name = "Bling-Bling King",
                            Description = "Gold-plated, diamond-studded King",
                            Category = "Chess",
                            Price = 1200
                        }
                    );
                    context.SaveChanges();
                }
            }
        }
    }

appsettings.json:

appsettings.json:


    "Data": {
        "SportStoreProducts": {
          "ConnectionString":  "Server=(localdb)\\MSSQLLocalDB;Database=SportsStore;TrustedConnection=True;MultipleActiveResultSets=true"
        }
      }

//startup.cs

// startup.cs


        public class Startup
        {
            IConfigurationRoot Configuration;
    
            public Startup(IHostEnvironment env)
            {
                Configuration = new ConfigurationBuilder()
                    .SetBasePath(env.ContentRootPath)
                    .AddJsonFile("appsettings.json").Build();
            }
    
    
    
            // This method gets called by the runtime. Use this method to add services to the container.
            // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
            public void ConfigureServices(IServiceCollection services)
            {
                services.AddDbContext<ApplicationDbContext>(options =>
                    options.UseSqlServer(
                        Configuration["Data:SportStoreProducts:ConnectionString"])); // loads configuration settings in the appsettings.json file and makes them available through a property called Configuration.
                    services.AddTransient<IProductRepository,
                    EFProductRepository>();
                services.AddMvc(options => options.EnableEndpointRouting = false);
            }
    
            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app, IServiceProvider serviceProvider, IWebHostEnvironment env)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                    app.UseStatusCodePages();
                    app.UseStaticFiles();
                }
    
                app.UseRouting();
    
                app.UseMvc(routes =>
                {
                    routes.MapRoute(
                        name: "default",
                        template: "{controller=Product}/{action=List}/{id?}");
                });
                SeedData.EnsurePopulated(app);
            }
        }
    }

//ApplicationDbContext.cs

// ApplicationDbContext.cs


    namespace SportsStore.Models
    {
        public class ApplicationDbContext : DbContext 
        {
            public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
                : base(options) {} // provides access to Entity Framework Core's underlying functionality
    
            public DbSet<Product> Products { get; set; } // Provides access to the Product objects in the database.
        }
    }

推荐答案

您的连接字符串中有错误.类型:

There is an error in your connection string. Type:

Trusted_Connection=True

这篇关于.Net错误:System.ArgumentException:'不支持的关键字:'trustedconnection'.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆