Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EF6在列更改后不工作[无效列]_C#_Entity Framework_Orm - Fatal编程技术网

C# EF6在列更改后不工作[无效列]

C# EF6在列更改后不工作[无效列],c#,entity-framework,orm,C#,Entity Framework,Orm,我的真实代码中有几个类,但对于这个问题,我将重点关注手头的两个类。我有一个List类,它包含名称和项。还有一个组类,它具有名称和项。以前的关系是列表->组(多个)->项。我们数据的默认值似乎更倾向于列表->项目(许多)->组,其中项目可以分组,也可以不分组。为了实现这一点,我只需从Group类中删除ListId和导航项。我可以很好地加载项目,但每当我尝试访问组DbSet时,它都会给我以下错误: 列名“List\u ListId”无效 下面是类和DbContext的代码。我使用的是EF6(不是核心

我的真实代码中有几个类,但对于这个问题,我将重点关注手头的两个类。我有一个List类,它包含名称和项。还有一个组类,它具有名称和项。以前的关系是列表->组(多个)->项。我们数据的默认值似乎更倾向于列表->项目(许多)->组,其中项目可以分组,也可以不分组。为了实现这一点,我只需从Group类中删除ListId和导航项。我可以很好地加载项目,但每当我尝试访问组DbSet时,它都会给我以下错误:

列名“List\u ListId”无效

下面是类和DbContext的代码。我使用的是EF6(不是核心)。我尝试过重新创建组类,重新启动Visual Studio,重新启动SQL Server,并擦除发布目录以确保其干净。帮忙

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;

namespace MyProject.Database
{
    [Table("dbo.Lists")]
    public class List
    {
        [Key]
        public int ListId { get; set; }

        [Required]
        public string Name { get; set; }

        [Required]
        public bool SortAsNumeric { get; set; }

        public virtual List<ListItem> Items { get; set; }
    }
}

您能否包括
列表
列表项
的代码?我怀疑您已经从一个类中删除了导航属性,而不是从另一个类中删除了List类,但是我将ListItem类添加到了原始文章中。我还从我的上下文中添加了一个正在工作的示例。它用于与项目一起加载的属性。您能否包含
列表
列表项目
的代码?我怀疑您已经从一个类中删除了导航属性,而不是从另一个类中删除了List类,但是我将ListItem类添加到了原始文章中。我还从我的上下文中添加了一个正在工作的示例。它用于与项目一起加载的属性。
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;

namespace MyProject.Database
{
    [Table("dbo.ListGroups")]
    public class ListGroup
    {
        [Key]
        public int ListGroupId { get; set; }

        [Required]
        public string Name { get; set; }

        [Required]
        public int OrderSequence { get; set; }

        [NotMapped]
        public List<ListItem> Items { get; set; }

        public ListGroup()
        {
            this.Items = new List<ListItem>();
        }
    }
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;

namespace MyProject.Database
{
    [Table("dbo.ListItems")]
    public class ListItem
    {
        [Key]
        public int ListItemId { get; set; }

        [Required]
        public string Label { get; set; }

        [Required]
        public string Value { get; set; }

        public string Subtext { get; set; }

        public string Icon { get; set; }

        public string Thumbnail { get; set; }

        public int OrderSequence { get; set; }

        [NotMapped]
        public bool Selected { get; set; }

        public int ListId { get; set; }

        public virtual List List { get; set; }

        public int? ListGroupId { get; set; }

        public virtual ListGroup Group { get; set; }

        public virtual List<ListItemProperty> Properties { get; set; }

        public ListItem()
        {
            this.OrderSequence = 1;
            this.Selected = false;
            this.Properties = new List<ListItemProperty>();
        }
    }
}
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web.Mvc;

namespace MyProject.Database
{
    public class ListContext : DbContext
    {
        public ListContext() : base("MyConnectionString") {}

        public virtual DbSet<List> Lists { get; set; }
        public virtual DbSet<ListItem> Items { get; set; }
        public virtual DbSet<ListGroup> Groups { get; set; }
        public virtual DbSet<ListItemProperty> Properties { get; set; }
        
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<List>()
                .HasMany(l => l.Items)
                .WithRequired(li => li.List)
                .HasForeignKey<int>(li => li.ListId);

            modelBuilder.Entity<ListItem>()
                .HasMany(li => li.Properties)
                .WithRequired(p => p.Item)
                .HasForeignKey<int>(p => p.ListItemId);
        }
    }
}
List MyList = Lists
    .Where(x => x.Name == ListName)
    .Include(x => x.Items.Select(i => i.Properties))
    .SingleOrDefault();