1

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>>(); } }
}
4
  • I think you'll need to add a but more context - firstly, the UnitOfWork code, and how, where and when you are calling the code you have posted. Commented Mar 15, 2013 at 18:46
  • 2
    Don't you need to make your properties virtual for lazy loading? Commented Mar 15, 2013 at 18:50
  • Won't work. If you're getting null back then virtual won't solve it. Commented Mar 15, 2013 at 18:53
  • You're right, too. But its good to know anyway. Commented Mar 15, 2013 at 18:55

3 Answers 3

1

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; }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I've made it like you said. The data exits now inside a employers table. The content of employers table shows me that the CompanyID and Company have the right reference. My Employee Collection inside the action is after reloading data still null.
you could also just have public Company Company { get; set; }
1

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

Comments

0

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 Employees? 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.

5 Comments

I've written these in notepad its not the orginal just for these exmaple. Second thing with the IObservableRepository works so far. My Table (its in the UOW, too) of type IObservableRepository for all employees have the data with the help of IronMan84. My problem is now that ObservableRepository inside my UOW class have the data from the table. My Collection object inside Companys class is null after reloading database.
I think you still don't add the employee to the company's employees. Or do you set employee.Company?
I will use a linq expression to include it there.
So the problem is solved? If not, please update your post with the current code. I don't see how a linq expression ties a company to an employee. The whole question boils down to: how do you set Employee.CompanyId in the database? Lazy loading is secondary.
I can inject via constructor my a reference of the dbset and query all employers to set the value of the collection inside my company class. Seems like a workaround ? I don't know.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.