Friday, 15 June 2012

Entity Framework 6 Code First: specific model design implementation -



Entity Framework 6 Code First: specific model design implementation -

i have little model have uncertainty on how design.

my thought have 3 classes: jobreport/customer/project. scope populate reports choosing client , project bound client (plus other payloads).

my design is:

model:

public class efdbcontext : dbcontext { public dbset<jobreport> jobreport { get; set; } }

when scaffold jobreport view , utilize model find difficoult bind client entity, since straight bound project , not jobreport. solution set both customerid , projectid jobreport, seems me it's redundant.

i have in mind 2 possibilities: 1- maintain first model , create viewmodel jobreport , client separately 2- utilize sec model have ids.

what think it? how implement case?

thanks much!!

max

a couple of observations:

design 1 seems appropriate, unless 1 jobreport should able reference multiple projects belonging different customers. the projectid property doesn't belong in client type, right? projects collection rightfully there, though. the project type should have jobreports collection lists reports belonging specific object.

this problem closely related discussions identifying aggregates in domain driven design. seems client entity natural aggregate root, means interactions objects within aggregate must go through customer.

this mean dbcontext should instead:

public class efdbcontext : dbcontext { public dbset<customer> customers { get; set; } }

and sub-items belonging customer, utilize linq traverse relations:

public ienumerable<jobreport> getreportsbycustomer(int customerid) { using (var context = new efdbcontext()) { homecoming context.customers.where(x => x.customerid.equals(customerid)) .selectmany(x => x.projects) .selectmany(x => x.jobreports) .orderby(x => x.timestamp); } }

you can select utilize client base of operations edit actions, since it's aggregate root. involve pulling client out of database , create sure detail info included:

public ienumerable<customer> getcustomer(int customerid) { using (var context = new efdbcontext()) { homecoming context.customers .include(x => x.projects.select(y => y.jobreports)) .firstordefault(x => x.customerid.equals(customerid)); } }

you need manually add together using system.data.entity; @ top of class file above code work.

entity-framework design ef-code-first

No comments:

Post a Comment