简体   繁体   中英

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

In mvc action

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

In 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.

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)

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. 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. So if you see using include in dbcontext you have made a left outer join between sites and pages.

You can check more on eager loading on 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. Problem was in entity constructor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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