Asp.net mvc 尝试插入带有外键的记录时无法使用LINQ保存?

Asp.net mvc 尝试插入带有外键的记录时无法使用LINQ保存?,asp.net-mvc,linq,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 R

我在网上找不到任何东西。我有一个
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; }
    }
}
在我的控制器中,我尝试创建一条记录

[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,
调试器告诉我,
无法将类型“int”隐式转换为“app.Models.Contact”
无法分别将类型“int”隐式转换为“app.Models.Reservation”
。我做错了什么?我的
预订
联系人
型号为

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; }

    }
}
以及


我什么都试过了。。我想也许我用错了LINQ?我在这里不知所措。

在课堂预订日期中,您需要具有联系人和预订表的导航属性。 添加联系人ID保留ID这样的属性

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; }
    }
 ContactId = _contact.Id
MVC会自动将该联系人IDFK绑定到联系人表PKID

保存时,您可以调用联系人ID,而不是联系人这样的对象

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; }
    }
 ContactId = _contact.Id

您的
Reservation
属性的类型为
Reservation
,但您正在尝试为其分配
int
(通过使用
Reservation=\u location.Id,
)。这个错误可以通过使用
Reservation=\u location,
来解决,但是您的模型确实应该包含
int ReservationId
导航属性(与
Contact
相同)