asp.net-mvc - 尝试使用外键插入记录时无法使用 LINQ 保存?
问题描述
我一直无法在网上找到任何东西。我有一个ReservationDate
模型,
namespace Mijem_test_app.Models
{
[HandleError]
/*
This model includes the location,
the person who reserved it and
FOR when they reserved it (not
when they reserved it)
*/
public class ReservationDate
{
//key
public int Id { get; set; }
//location name
[Required]
public Reservation Reservation { get; set; }
//contact
[Required]
public Contact Contact { get; set; }
//date reserved for
[Required]
public DateTime ReservedDate { get; set; }
//information from textbox
[Required]
public string InfoFromTextBox { get; set; }
public bool Deleted { get; set; }
public ImagesUploaded ImageURL { get; set; }
}
}
在我的控制器中,我尝试CREATE
记录,
[HttpPost]
public ActionResult Save(ReservationDate reservation)
{
var _contactID = (int) TempData["ContactID"];
var _contact = _context.Contacts
.Single(r => r.Id == _contactID);
var _location = _context.Reservations
.Single(r => r.Name == reservation.Reservation.Name);
var _ReservedDate = (DateTime)TempData["BookDate"];
var _InfoFromTextBox = (string)TempData["InfoFromTextBox"];
var __reservation = new ReservationDate
{
ReservedDate = _ReservedDate,
Contact = _contact.Id,
Reservation = _location.Id,
InfoFromTextBox = _InfoFromTextBox,
Deleted = false
};
_context.ReservationDates.Add(__reservation);
_context.SaveChanges();
return View("Index");
}
代码在
Contact = _contact.Id,
Reservation = _location.Id,
调试器分别告诉我Cannot implicitly convert type 'int' to 'app.Models.Contact
和Cannot implicitly convert type 'int' to 'app.Models.Reservation
。我究竟做错了什么?我Reservation
和Contact
模特是,
namespace app.Models
{
[HandleError]
public class Reservation
{
//unique ID of location
public int Id { get; set; }
//name of location
[Required]
[StringLength(255)]
public string Name { get; set; }
public int Ranking { get; set; }
public bool Favorites { get; set; }
}
}
和,
namespace app.Models
{
[HandleError]
public class Contact
{
public int Id { get; set; }
[Required]
[StringLength(255)]
[Display(Name = "Contact Name ...")]
public string Name { get; set; }
[Required]
[StringLength(255)]
[Display(Name = "Contact Type")]
public string ContactType { get; set; }
[Required]
[Display(Name = "Phone:")]
public long ContactNumber { get; set; }
[Column(TypeName = "date")]
[Display(Name = "Birthdate")]
public DateTime BirthDate { get; set; }
}
}
我已经尝试了一切..我想也许我错误地使用了 LINQ?我在这里不知所措。
解决方案
在 ReservationDate 类中,您需要具有 Contact 和 Reservation 表的导航属性。像这样添加ContactId和ReservationId属性
public class ReservationDate
{
//key
public int Id { get; set; }
public int ReservationId { get; set; } // this
//location name
[Required]
public Reservation Reservation { get; set; }
public int ContactId { get; set; } // this
//contact
[Required]
public Contact Contact { get; set; }
//date reserved for
[Required]
public DateTime ReservedDate { get; set; }
//information from textbox
[Required]
public string InfoFromTextBox { get; set; }
public bool Deleted { get; set; }
public ImagesUploaded ImageURL { get; set; }
}
MVC 自动将该ContactID FK 绑定到 Contact 表 PK ID。
在保存时,您调用ContactId而不是像这样的Contact对象
ContactId = _contact.Id
推荐阅读
- asp.net-core - 无法从“TableDependency.SqlClient.Base.Enums.DmlTriggerType”转换为“TableDependency.SqlClient.Base.Abstracts.ITableDependencyFilter”
- python - np.ptp 的弃用警告
- javascript - 在 Nuxt for AMP 中预加载/渲染 ajax 请求
- ruby-on-rails - 带有 Google 适配器的 Chartkick 在 Heroku Production 上卡住了“正在加载...”
- javascript - 在新窗口中打开 window.location.href 位置
- express-session - sql server 数据库中的 express-session 存储会话
- wso2esb - wso2 EI:自定义数据源
- typescript - Angular:定义模型并用空值声明对象
- visual-studio-code - 从 Netbeans 迁移到 VSCode
- r - 如何运行 for 循环以通过虚拟变量运行回归