我正在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);
}
预订视图模型
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; }
}
最佳答案
这可能有助于:
http://www.entityframeworktutorial.net/code-first/foreignkey-dataannotations-attribute-in-code-first.aspx
您可以使用数据注释来定义密钥,并将虚拟关键字放在国家的前面,如下所示:
[ForeignKey("Country")]
public Int16 CountryID { get; set; }
public virtual Country Country { get; set; }
您还需要添加一个
using System.ComponentModel.DataAnnotations.Schema;
。关于c# - 在单独的表EF Core中插入2个项目,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41286784/