我在NETCore上预订申请
预订与入住率有一对多的关系
两种型号都具有导航属性
似乎外键(OccupancyId
)未在Booking
中设置。
编辑:以下代码似乎运行正常。
Occupancy newOccupancy = new Occupancy { AccommodationId = 12, DateIn = new DateTime(2017, 1, 18), DateOut = new DateTime(2017, 2, 18) };
ApplicationDbContext.Occupancies.Add(newOccupancy);
Booking newBooking = new Booking { AccommodationId = 12, BookingStatusId = 6, Capacity = 69, OccupancyId = newOccupancy.Id, Paid = true, ProfileId = 1, Remark = "Geen", Total = 0 };
ApplicationDbContext.Bookings.Add(newBooking);
但是,当使用此代码时,它似乎不再起作用
ApplicationDbContext.Occupancies.Add(model.Occupancy);
ApplicationDbContext.Bookings.Add(model.Booking);
在BookingController中创建方法
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(CityViewModel model)
{
var alert = new Alert();
try
{
if(!ModelState.IsValid) {
alert.Message = alert.ExceptionMessage = ApplicationDbContextMessage.INVALID;
throw new Exception();
}
Occupancy newOccupancy = new Occupancy { AccommodationId = 12, DateIn = new DateTime(2017, 1, 18), DateOut = new DateTime(2017, 2, 18) };
ApplicationDbContext.Occupancies.Add(newOccupancy);
Booking newBooking = new Booking { AccommodationId = 12, BookingStatusId = 6, Capacity = 69, OccupancyId = newOccupancy.Id, Paid = true, ProfileId = 1, Remark = "Geen", Total = 0 };
ApplicationDbContext.Bookings.Add(newBooking);
if (await ApplicationDbContext.SaveChangesAsync() == 0)
{
alert.Message = alert.ExceptionMessage = ApplicationDbContextMessage.CREATENOK;
throw new Exception();
}
alert.Message = ApplicationDbContextMessage.CREATEOK;
alert.Type = AlertType.Success;
AddAlert(alert);
return RedirectToAction("Index");
}
catch(Exception ex)
{
alert.Type = AlertType.Error;
alert.ExceptionMessage = ex.Message;
//AddAlert(alert);
model = await ViewModel(model.City);
ModelState.AddModelError(string.Empty, alert.ExceptionMessage);
}
return View(model);
}
BookingViewModel
public class BookingViewModel
{
public Booking Booking { get; set; }
public List<SelectListItem> Accommodations { get; set; }
public List<SelectListItem> BookingStatuses { get; set; }
public Occupancy Occupancy { get; set; }
}
占用模型
public class Occupancy : BaseEntity<Int64>
{
public DateTime DateIn { get; set; }
public DateTime DateOut { get; set; }
public OccupancyType OccupancyType { get; set; }
public Int64 AccommodationId { get; set; }
public Accommodation Accommodation { get; set; }
public Booking Booking { get; set; }
}
预订模式
public class Booking : BaseEntity<Int64>
{
public int Capacity { get; set; }
public bool Paid { get; set; }
public Decimal Total { get; set; }
public string Remark { get; set; }
public Int64 ProfileId { get; set; }
public Profile Profile { get; set; }
public Int64 AccommodationId { get; set; }
public Accommodation Accommodation { get; set; }
public Int64 OccupancyId { get; set; }
public Occupancy Occupancy { get; set; }
public Int64 BookingStatusId { get; set; }
public BookingStatus BookingStatus { get; set; }
}
答案 0 :(得分:2)
您的密钥可以通过使用数据注释来定义,并将虚拟关键字放在国家/地区的前面,如下所示:
[ForeignKey("Country")]
public Int16 CountryID { get; set; }
public virtual Country Country { get; set; }
您还需要添加using System.ComponentModel.DataAnnotations.Schema;
。
答案 1 :(得分:0)
我似乎已经通过在我的控制器中使用它解决了这个问题
ApplicationDbContext.Occupancies.Add(model.Occupancy);
model.Booking.OccupancyId = model.Occupancy.Id;
ApplicationDbContext.Bookings.Add(model.Booking);
所以我认为导航属性出了问题,不确定是什么,但这似乎暂时解决了问题。