首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >actionresult上的invalidoperationexception actionresult

actionresult上的invalidoperationexception actionresult
EN

Stack Overflow用户
提问于 2016-06-21 15:15:58
回答 1查看 85关注 0票数 0

我正在尝试将三个数据库表中的一些数据放入一个视图中。因此,我使用一个类,其中包含另外两个带有列表的类。

这两个包含的类中的一个运行良好,但另一个在控制器ActionResult中导致InvalidOperationException。

这是我到目前为止产生的结果:

代码语言:javascript
复制
namespace ArrangeAppointments.Models
{
    public class Appointment : IAppointment
    {
        #region Implementation of IAppointments
            public int UserId { get; set; }
            public string Title { get; set; }
            public string Description { get; set; }
            public string Duration { get; set; }
            public virtual List<Invitee> Invitees { get; set; }
            public virtual List<Occasion> Occasions { get; set; }
        #endregion
        public int AppointmentId { get; set; }


namespace ArrangeAppointments.Models
{
    public class Invitee : IInvitee
    {
        public int Id { get; set; }
        public int AppointmentId { get; set; }
        public string Email { get; set; }
        public string Presence { get; set; }
        public virtual Appointment Appointment { get; set; }


namespace ArrangeAppointments.Models
{
    public class Occasion : IOccasion
    {
        public int Id { get; set; }
        public int AppointmentId { get; set; }
        public DateTime ocDate { get; set; }
        public DateTime ocTime { get; set; }
        public string Location { get; set; }
        public virtual Appointment Appointment { get; set; }

ActionResult

代码语言:javascript
复制
[HttpGet]
public ActionResult NewApp(int Id)
{
    if (Id == 0)
    {
        return View();
    } else {
        try
        {
            using (var db = new MainDbContext())
            {
                Appointment appointment = db.Appointments.Find(Id);
                if (appointment == null)
                {
                    return HttpNotFound();
                }
                else
                {
                    return View("NewApp", appointment);
                }
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
            Console.WriteLine(e.InnerException);
            return View();
        }
    }
}

ActionResult中的对象'appointment‘填充了所有必需的数据,但用于场合的列表除外。这会导致异常。但是,此异常不会触发catch事件。

是什么导致了这种异常?

DbContext类

代码语言:javascript
复制
namespace ArrangeAppointments
{
    public class MainDbContext : DbContext
    {
        public MainDbContext() : base("name=DefaultConnection")
        {
        }

        public DbSet<User> Users { get; set; }

        public DbSet<Appointment> Appointments { get; set; }

        public DbSet<Invitee> Invitees { get; set; }

        public DbSet<Occasion> Occasions { get; set; }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2016-06-21 15:34:39

您需要包括列表

代码语言:javascript
复制
 Appointment appointment = db.Appointments.include("Occasions").include("Invitees").SingleOrDefault(a => a.AppointmentId == Id);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37937825

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档