I am trying to insert data from my ASP.NET Core MVC application with Entity Framework to my SQL Server database on localhost.
My model class looks like this:
public class Auto
{
public string Motorleistung { get; set; }
public string Lackierung { get; set; }
public string Felgen { get; set; }
public string Sonderleistungen { get; set; }
}
I already added the DbContext in a new folder (Services/AutoContext class):
public class AutoContext : DbContext
{
DbSet<Auto> Autos { get; set; }
public AutoContext(DbContextOptions<AutoContext> options)
: base(options)
{
Database.EnsureCreated();
}
}
I added this part to the Startup.cs file in the ConfigurateServices method:
public void ConfigureServices(IServiceCollection services)
{
var connectionString = "Server=localhost;Database=Auto;User Id=sa;Password=YourPassword123";
services.AddControllersWithViews();
services.AddDbContext<AutoContext>(o => o.UseSqlServer(connectionString));
}
I am trying to use a class extension with a method to insert properties from auto into the DB:
public static class AutoContextExtensions
{
public static void CreateSeedData(this AutoContext context)
{
var auto = new List<Auto>()
{
new Auto()
{
Motorleistung = "500 PS",
Lackierung = "Gelb",
Felgen = "Chrome",
Sonderleistungen = "Sitzheizung"
}
};
context.AddRange(auto);
context.SaveChanges();
}
}
I am now trying to call the CreateSeedData function from the Startup.cs to pass the data into my database like:
Projectname.AutoContextExtensions.CreateSeedData();
It expects me to give a parameter. What parameter do I have to pass? (I extracted the code from a sample)