Sunday, 15 April 2012

c# - context.Entities.Create() with default constructor -



c# - context.Entities.Create() with default constructor -

when using entity framework dbcontext, create new entity , wrap in proxy class, 1 :

var product = context.products.create();

that creates proxy entity, doesn't allow contructor variable initialization :

var product = new product { name = "product name" };

question 1 : there way create proxy instanciated object ? :

context.products.createproxyfrom(product)

question 2 : there way create class initialization

context.products.create() { name = "product name" };

i know both alternative not exist, i'm wondering there shortcut or workaround around this.

question 3:

having paymentcheck : payment (inheritance) can context.payments.create(); need paymentcheck instance. how can create of paymentcheck ?

i don't see big deal setting property values after proxy has been created, if absolutely must have property value setting on same line , aren't afraid pollute intellisense bit, can bit functional extension methods:

public static t apply<t>(this t input, action<t> action) { action(input); homecoming input; }

usage:

product product = context.products.create() .apply(p => p.name = "product name") .apply(p => p.category = 42);

c# entity-framework dbcontext

No comments:

Post a Comment