locked
EF: How to add computed column refering to another table. RRS feed

  • Question

  • Hi,

    I am using VS2010, .net 4.0 and ef 4.0 with Database First approach. and Learning EF.

    I have Classic Department(DeptNo,Name) and Employee(EmpNo,EmpName, ...... FKDeptNo) Tables.

    I now wish to have a computed column in Department Table named "TotalEmployees"  that should contain respective department's Total number of Employees.

    Possible ? How ?

    Regards

    Wednesday, December 19, 2012 9:59 AM

Answers

  • Hi Jiya,

    Welcome to the MSDN forum.

    You are using database approach. I never recommend you edit the current entity data model when you are using database approach. Since if you want to update from database again, all the changes you made to entity data model will be lost. If you generate database from current model, your database will be dropped and recreated which will make data and information lose. I am not sure the relationship between Department and Employee (one to many?). You can try this to count the total number of Employees of a Department:

    var num = context.Departments.Where(p => p.Id == 1).First().Employees.Count();

    Have a nice day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Alexander Sun Wednesday, December 26, 2012 3:02 AM
    Friday, December 21, 2012 2:36 AM