views:

33

answers:

1

Has anyone advice when working with large database models when using EF4 code first? Entity sets are added to the database context, but if I have 100 tables, I need to create 100 DbSets, 1 for each table:

public class Customers : DbContext
{
    public DbSet<Customer> Customers {get; set;}
    public DbSet<Employees> Employees {get; set;}
    public DbSet<...
    ...
    ...
    95 more
}

With large database models, is it better to split the DbSets in to multiple classes depending on their domain?

+2  A: 

Yes you have to create POCO class and DbSet for each table. That is the meaning of Code first. If you don't like it you can use standard EDMX and let EF generate entities and mapping for you.

Splitting model to multiple contexts can be good idea but it depends on your domain model and on other characteristics of your solution. Be aware that many features like lazy loading and complex queries works only on entities attached to the same context. Only entites mapped in the context can be used there.

For such a big solution you can check domain driven design theory about aggregate roots and repositories. It can give you some ideas about splitting your solution.

Ladislav Mrnka