DespeiL
DespeiL

Reputation: 1033

EF 6.1.3 Foreign key assosiation

I'm using EF6 to generate model from Database .

  public partial class FolderFiles
    {
        public int ID { get; set; }
        public string FileName { get; set; }
        public virtual Folders FolderID{ get; set; }
    }

    public partial class Folders
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public FoldersTreeDict()
        {
            this.FoldersTreeDict1 = new HashSet<FoldersTreeDict>();
            this.FolderFiles = new HashSet<FolderFiles>();
        }

        public int ID { get; set; }
       public string Name { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<FolderFiles> FolderFiles { get; set; }
    }

After update on getting result as list, EF load all date from FolderFiles where FolderID=is ID of updated Folder. It's take a lot of time and I don't need them in this case at all

Here is the update:

       var model = db.Folders;
                if (ModelState.IsValid)
                {
                    try
                    {

                        var modelItem = model.SingleOrDefault(x => x.ID == item.ID);
                        if (modelItem != null)
                        {

                            modelItem.Name = "Test";

                            db.SaveChanges();
                        }
                    }
                    catch (Exception e)
                    {
                        ViewData["EditError"] = e.Message;
                    }
                }
var result = model.ToList();

What I'm missing or doing wrong in this update so foreign data is load?

P.S In other framework which I worked with on other language there were 2 ways to load foreign data 1) on lading object ( i think it calls lazy load) 2) when we want to get data from association property

Upvotes: 1

Views: 64

Answers (1)

Stephen Reindl
Stephen Reindl

Reputation: 5819

You may use

db.Configuration.LazyLoadingEnabled = false

which sets all following selects to not load any referenced objects. You may have to load them manually.

Upvotes: 2

Related Questions