首页 > 解决方案 > 尝试使用外键插入记录时无法使用 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.ContactCannot implicitly convert type 'int' to 'app.Models.Reservation。我究竟做错了什么?我ReservationContact模特是,

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?我在这里不知所措。

标签: asp.net-mvclinq

解决方案


在 ReservationDate 类中,您需要具有 Contact 和 Reservation 表的导航属性。像这样添加ContactIdReservationId属性

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

推荐阅读