orm - Entity splitting with optional relation - (EF code first) -


i have 2 tables: orders table , orderactivity table. if no activity has been taken on order, there no record in orderactivity table. have orderactivity table mapped optional nav property on order entity , handle updates orderactivity this:

if (order.orderactivity == null) {     order.orderactivity = new orderactivity(); } order.orderactivity.lastaccessdatetime = datetime.now; 

is possible consolidate such columns of orderactivity table mapped properties on orders entity, , default if there no orderactivity record? configuration entity splitting appears work if records exist in both tables. if not possible, best practice obscure child entity domain model? goal keep model clean possible while interacting db schema have no control over.

you can create mapping , specify type of lastaccessdate nullable<datetime>. mapping create one-to-one lastaccessdate being optional.

public class order {   [key]    public int id { get; set; }   public string name { get; set; }   public datetime? lastaccessdate { get; set; } }  modelbuilder.entity<order>().map(m => {           m.properties(a => new { a.id, a.name });           m.totable("order");         }).map(m => {           m.properties(b => new { b.id, b.lastaccessdate });           m.totable("orderactivity");         }); 

in case, specifying lastaccessdate property optional when inserting new orders.

var order = new order(); order.name = "orderwithactivity"; order.lastaccessdate = datetime.now; db.orders.add(order); db.savechanges();  order = new order(); order.name = "orderwithoutactivity"; db.orders.add(order); db.savechanges(); 

note create 1 entry in each table. necessary because ef creates inner join when retrieve orders , want orders in case. lastaccessdate either have value or null.

// gets both orders var order = db.orders.tolist(); // gets 1 activity var orders = db.orders.where(o => o.lastaccessdate != null); 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -