我正在try 使用ASP.NET Core API和Angular创建预订类型网站.
我被模特/关系困住了.
这是Hotel
模型类:
public class Hotel
{
[Key]
public int HotelId { get; set; }
// Hotel name
[Column(TypeName = "nvarchar(50)")]
public string HotelName { get; set; }
[Range(1,5,ErrorMessage = "Hotel Star Rating Value Must be in between of 1 and 5.")]
public byte Stars { get; set; }
// Hotel address linked to Hotel class.
public virtual ICollection<Address> HotelAddresses { get; set; }
// Hotel rooms linked to hotel class.
public virtual ICollection<Room> Rooms { get; set; }
}
这是Address
模型类:
public class Address
{
[Key]
public int AddressId { get; set; }
[Column(TypeName = "nvarchar(50)")]
public string Country { get; set; }
[Column(TypeName = "nvarchar(50)")]
public string City { get; set; }
[Column(TypeName = "nvarchar(50)")]
public string Street { get; set; }
// one-to-many.
[ForeignKey("hotel")]
public int HotelId { get; set; }
public virtual Hotel hotel { get; set; }
}
这是Room
模型类:
public class Room
{
[Key]
public int RoomId { get; set; }
[Column(TypeName ="nvarchar(50)")]
public string Name { get; set; }
[Column(TypeName = "nvarchar(250)")]
public string Description { get; set; }
public decimal price { get; set; }
// one-to-many
[ForeignKey("hotel")]
public int HotelId { get; set; }
public virtual Hotel hotel { get; set; }
}
控制器动作方法:
[HttpPost]
public async Task<ActionResult<Hotel>> PostHotel(Hotel hotel)
{
_context.Hotels.Add(hotel);
await _context.SaveChangesAsync();
return CreatedAtAction("GetHotel", new { id = hotel.HotelId }, hotel);
}
所以问题是:我做错了什么?
如果没有的话,当我try 拥有外部密钥之类的东西时,我应该如何手动创建记录呢?
我try 以不同的方式创建一对多关系,很多次,我认为这是关于我的代码错误的,我认为通过修复我的模型类Swagger UI将使创建新记录变得更容易(基本上是执行发布方法),但当Swagger给我的格式时,我感到困惑,认为我写wine 店模型和ID太像它们不应该自动化吗?