简体   繁体   English

实体框架6获取表中不存在的记录

[英]Entity Framework 6 take records that don't exist in table

In mvc action 在MVC动作中

Site site = siteRepository.UserSites(userId).Where(x => x.Order == siteOrder).Last();

In SiteRepository 在SiteRepository中

 public IEnumerable<Site> UserSites(string userId)
 {
     return context.Sites.Include("Pages").Where(x => x.UserId == userId);
 }

and other repositories 和其他存储库

public IEnumerable<Row> Items
{
    get
    {
        return context.Rows;
    }
}

public IEnumerable<Page> Items
{
    get
    {
        return context.Pages;
    }
 }

 public IEnumerable<AbstractBuildBlock> Items
 {
     get
     {
         return context.BuildBlocks;
     }
 }

I don't know why, but in debugger I see that after request to UserSites(the first repository code snippet), In Site.Rows there is one more record, it is the first record and it created by Row constructor, I haven't any ideas why it happens. 我不知道为什么,但是在调试器中,我看到对UserSites的请求(第一个存储库代码段)之后,在Site.Rows中还有一个记录,这是第一个记录,它是由Row构造函数创建的,我还没有任何想法为什么会发生。

These two objects have different types the first(that I don't understand) has type Domain.Entities.Row , the second System.Data.Entity.DynamicProxies.Row_********** (* - some num) 这两个对象具有不同的类型,第一个(我不了解)的类型是Domain.Entities.Row ,第二个System.Data.Entity.DynamicProxies.Row_********** (*-一些数字)

I'll repeat one more time. 我再重复一遍。 In my Rows table there is only one record, but in Site.Pages[someIndex].Rows after request to database there are two objects and the first doesn't exist in table, it created by Row constructor. 在我的Rows表中只有一个记录,但是在Site.Pages [someIndex]中。向数据库请求后的行有两个对象,第一个在表中不存在,它是由Row构造函数创建的。 Why it happens? 为什么会发生?

Code of Site class 网站分类代码

public class Site
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    [Required(ErrorMessage ="Please, enter the site local name")]
    [Display(Name ="Local name")]
    public string LocalName { get; set; }
    [Required(ErrorMessage = "Please, enter the site public name")]
    [Display(Name ="Public name")]
    public string PublicName { get; set; }
    [Display(Name ="Menu variant")]
    public string MenuVariant { get; set; }
    [Display(Name = "Color theme")]
    public string ColorTheme { get; set; }
    public string IconUrl { get; set; }
    public string UserId { get; set; }
    public int Order { get; set; }
    public int NextPageOrder { get; set; }
    public virtual List<TopMenuItem> TopMenuItems { get; set; }
    public virtual List<LeftMenuItem> LeftMenuItems{ get; set; }
    public virtual List<Page> Pages { get; set; }
    public Page ActivePage { get; set; }
    public int ActivePageIndex { get; set; }

    public Site()
    {
        NextPageOrder = 1;
        MenuVariant = "top";
        ColorTheme = "dark";
    }
}

page class 页面类

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public string Title { get; set; }
    public string Name { get; set; }
    public virtual List<Row> Rows { get; set; }
    public int SiteId { get; set; }
    public virtual Site Site { get; set; }
    public int Order { get; set; }
    public int NextRowOrder { get; set; }
    public Page()
    {
        Title = "title";
        Rows = new List<Row>()
        {
            new Row()
        };
    }

First I would assume that your sites and pages has some relationships. 首先,我假设您的网站和页面具有某种关系。 When you are trying to fetch data from sites table you are getting data from pages table as well as you have included pages in the dbcontext query. 当您尝试从站点表中获取数据时,您正在从页面表中获取数据,并且已在dbcontext查询中包含了页面。 So if you see using include in dbcontext you have made a left outer join between sites and pages. 因此,如果您看到在dbcontext中使用include,则表示站点和页面之间已进行左外部联接。

You can check more on eager loading on http://www.entityframeworktutorial.net/EntityFramework4.3/eager-loading-with-dbcontext.aspx . 您可以在http://www.entityframeworktutorial.net/EntityFramework4.3/eager-loading-with-dbcontext.aspx上查看有关急切加载的更多信息 Let me know if it solves your query. 让我知道它是否可以解决您的查询。

See solution in comments to the question, big thanks to Ivan Stoev. 在问题评论中查看解决方案,非常感谢Ivan Stoev。 Problem was in entity constructor. 问题出在实体构造函数中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM