我正在尝试使用 .NET 6 创建自己的 Web API,但在将数据放入数据库之前,但出现错误
System.InvalidOperationException:“无法创建“Hotelum.HotelsSeeder”类型的实例,因为无法绑定一个或多个参数。构造函数参数必须具有相应的属性。不支持字段。缺少的属性是:“dbContext”
using Hotelum.Entities;
using Microsoft.EntityFrameworkCore;
namespace Hotelum
{
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddTransient<IWeatherForecastService, WeatherForecastService>();
builder.Services.AddControllers();
builder.Services.AddDbContext<HotelsDbContext>();
builder.Services.AddScoped<HotelsSeeder>();
var app = builder.Build();
// Configure the HTTP request pipeline.
app.Configuration.Get<HotelsSeeder>(); //Error appears
HotelsSeeder seeder = app.Services.GetRequiredService<HotelsSeeder>();
seeder.Seed();
app.UseHttpsRedirection();
//app.UseAuthorization();
app.MapControllers();
app.Run();
}
}
}
我已经寻找任何解决方案但找不到它。
这是数据所在的类
using Hotelum.Entities;
namespace Hotelum
{
public class HotelsSeeder
{
private readonly HotelsDbContext _dbContext;
public HotelsSeeder(HotelsDbContext dbContext)
{
_dbContext = dbContext;
}
public void Seed()
{
if (_dbContext.Database.CanConnect())
{
if (!_dbContext.Hotels.Any())
{
var hotel = GetHotels();
_dbContext.Hotels.AddRange(hotel);
_dbContext.SaveChanges();
}
}
}
private IEnumerable<Hotels> GetHotels()
{
var hotel = new List<Hotels>();
new Hotels()
{
Name = "Avocado",
Description = "Perfect for family, chill and drinks",
Category = "Holidays",
IsItOpen = true,
ContactEmail = "[email protected]",
ContactNumber = "123456789",
Rooms = new List<Rooms>()
{
new Rooms()
{
Name = "Król",
Description = "Dla bogatych",
Price = 8549098M,
NumberOfRooms = 1,
},
new Rooms()
{
Name = "Królowa",
Description = "Dla mniej bogatych",
Price = 89098M,
NumberOfRooms = 5,
},
new Rooms()
{
Name = "Standart",
Description = "Dla standartów",
Price = 88M,
NumberOfRooms = 25,
},
},
Address = new Address()
{
City = "Wwa",
Street = "Poznanska",
PostalCode = "10-092"
}
};
new Hotels()
{
Name = "Abada",
Description = "Perfect for horrors",
Category = "Horros",
IsItOpen = true,
ContactEmail = "[email protected]",
ContactNumber = "123456789",
Rooms = new List<Rooms>
{
new Rooms()
{
Name = "Bieda",
Description = "Dla odważnych",
Price = 8M,
NumberOfRooms = 1,
},
new Rooms()
{
Name = "Mniejsza bieda",
Description = "Dla normalnych",
Price = 98M,
NumberOfRooms = 5,
},
new Rooms()
{
Name = "Bogato",
Description = "Dla bogatych",
Price = 88M,
NumberOfRooms = 25,
}
},
Address = new Address()
{
City = "Poznan",
Street = "Warszawska",
PostalCode = "50-792"
}
};
return hotel;
}
}
}
我尝试通过配置来实现它,但找不到使用它的方法,试图添加
AddDbContext
,因为它要么不起作用。
这里有一些问题,首先是你的
Program.cs
。你应该正确使用DI
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
try
{
var seeder = services.GetRequiredService<HotelsSeeder>();
seeder.Seed();
}
catch (Exception ex)
{
// Handle any exceptions while seeding
Console.WriteLine($"An error occurred while seeding the database: {ex.Message}");
}
}
HotelsSeeder
应在申请范围内由服务提供商解决。 using
语句用于确保在完成对 .seed()
的调用后正确释放作用域。
在您的方法
GetHotels()
中,您没有将Hotels
对象添加到要播种的列表hotel
中。
private IEnumerable<Hotels> GetHotels()
{
var hotel = new List<Hotels> {
new Hotels()
{
Name = "Avocado",
Description = "Perfect for family, chill and drinks",
Category = "Holidays",
IsItOpen = true,
ContactEmail = "[email protected]",
ContactNumber = "123456789",
Rooms = new List<Rooms>()
{
new Rooms()
{
Name = "Król",
Description = "Dla bogatych",
Price = 8549098M,
NumberOfRooms = 1,
},
new Rooms()
{
Name = "Królowa",
Description = "Dla mniej bogatych",
Price = 89098M,
NumberOfRooms = 5,
},
new Rooms()
{
Name = "Standart",
Description = "Dla standartów",
Price = 88M,
NumberOfRooms = 25,
},
},
Address = new Address()
{
City = "Wwa",
Street = "Poznanska",
PostalCode = "10-092"
}
},
new Hotels()
{
Name = "Abada",
Description = "Perfect for horrors",
Category = "Horros",
IsItOpen = true,
ContactEmail = "[email protected]",
ContactNumber = "123456789",
Rooms = new List<Rooms>
{
new Rooms()
{
Name = "Bieda",
Description = "Dla odważnych",
Price = 8M,
NumberOfRooms = 1,
},
new Rooms()
{
Name = "Mniejsza bieda",
Description = "Dla normalnych",
Price = 98M,
NumberOfRooms = 5,
},
new Rooms()
{
Name = "Bogato",
Description = "Dla bogatych",
Price = 88M,
NumberOfRooms = 25,
}
},
Address = new Address()
{
City = "Poznan",
Street = "Warszawska",
PostalCode = "50-792"
}
}};
return hotel;
}