简体   繁体   中英

Entity Framework lazy loading problems

I like to have my repository of employers inside my company class and inside my unit of work class. I've tried the test code below. My Employers Repository null after reloading data from database.
I have lazy loading active.

public class Company
{
    public int Id { get; set; }
    public string CompanyName { get; set; }
    public ICollection<Employee> Employers { get; set; }
}

public class Employee
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string SureName { get; set; }
}   

TEST CODE :

var company = new Company();

var employee1 = new Employee();
employee.Name = "myFirst employee";

unitOfWork.companys.Add(companys);
unitOfWork.Commit();

unitOfWork.companys.Employers.Add(employee1);

uow.Commit();

If i get these to work another question shoud be: it possible to use my IObservableRepository(implements ICollection) instead of ICollection for my Employers. I also want to put all my employers from all companys together like a single entity.

public class UnitOfWork
{
    public IObservableRepository<Company> Company { get { return GetRepo<IObservableRepository<Company>>(); } }
    public IObservableRepository<Employee> Employers { get { return GetRepo<IObservableRepository<Employee>>(); } }
}

It looks to me like you haven't actually created the relationship between Company and Employee in your code files yet. You've got the Company part down--with the collection of employees--but you need to include a couple of properties on Employee as well to finalize the relationship:

public class Employee
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string SureName { get; set; }

    [ForeignKey("Company"), Required]
    public int CompanyID { get; set; }

    public virtual Company Company { get; set; }
}

ProxyCreationEnabled was disabled. Thanks IronMan84 to solve the other problem.

First allow me to say that your code is a bit messy. UnitOfWork has Company (singular) and Employers (plural). And you do unitOfWork.companys.Add (lower case), and unitOfWork.companys.Employers.Add (??). And... an Employers collection consisting of Employee s? I assume that these are typos.

So if I read you well, you insert a Company and an Employee but never relate the two. You should have a statement like

company.Employees.Add(employee1);

is it possible to use my IObservableRepository(implements ICollection) instead of ICollection for my Employers

No, it isn't. EF is not going to create an IObservableRepository<Employee> for you. It would not even know which concrete class it should use in the first place, but it needs an interface that is implemented by EntityCollection or collection classes like HashSet or List .

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