.Net error: System.ArgumentException: ‘Keyword not supported: ‘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

    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:
    "Data": {
        "SportStoreProducts": {
          "ConnectionString":  "Server=(localdb)\MSSQLLocalDB;Database=SportsStore;TrustedConnection=True;MultipleActiveResultSets=true"
        }
      }

// 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
    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.
        }
    }

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

There is an error in your connection string. Type:

Trusted_Connection=True


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x