简体   繁体   English

实体框架延迟加载问题

[英]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. 如果我让它们起作用,那么另一个问题应该是:可以为我的雇主使用IObservableRepository(implements ICollection)而不是ICollection。 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. 在我看来,您尚未在代码文件中实际创建Company与Employee之间的关系。 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: 您已经使Company的员工人数减少了-但是您还需要在Employee上包括几个属性以最终确定关系:

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. ProxyCreationEnabled已禁用。 Thanks IronMan84 to solve the other problem. 感谢IronMan84解决了另一个问题。

First allow me to say that your code is a bit messy. 首先,请允许我说您的代码有点混乱。 UnitOfWork has Company (singular) and Employers (plural). UnitOfWorkCompany (单数)和Employers (复数)。 And you do unitOfWork.companys.Add (lower case), and unitOfWork.companys.Employers.Add (??). 然后执行unitOfWork.companys.Add (小写)和unitOfWork.companys.Employers.Add (??)。 And... an Employers collection consisting of Employee s? 还有...由Employee组成的Employers集合? I assume that these are typos. 我认为这些是错别字。

So if I read you well, you insert a Company and an Employee but never relate the two. 因此,如果我读得很好,您会插入CompanyEmployee但永远不要将两者联系起来。 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 是否可以为我的雇主使用IObservableRepository(implements ICollection)而不是ICollection

No, it isn't. 不,不是。 EF is not going to create an IObservableRepository<Employee> for you. EF不会为您创建IObservableRepository<Employee> 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 . 它甚至不知道首先应该使用哪个具体类,但是它需要一个由EntityCollectionHashSetList类的集合类实现的接口。

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

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