How to: Create an Object Using the Static Create Method

[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

The Entity Framework tools use conceptual schema definition language (CSDL) to generate code that defines the object layer. When the data classes are generated, each class is generated with a static create factory method. This method instantiates an object and sets all the properties of the class that cannot be null. The method includes a parameter for every property that has the Nullable="false" attribute applied in the CSDL, has no default value defined in the model, and that is accessible to the Entity Framework. The following example shows how to specify a default value and the set accessor on a property in the conceptual model.

Using this property definition in the conceptual model, the code generator will generate an entity with a CarrierTrackingNumber property that has a default value "1A-2B-3C" and a protected set accessor.

Use this method when creating objects with many required properties.

The example in this topic is based on the Adventure Works Sales Model. To run the code in this topic, you must have already added the Adventure Works Sales Model to your project and configured your project to use the Entity Framework. For more information, see How to: Use the Entity Data Model Wizard (Entity Framework) or How to: Manually Configure an Entity Framework Project and How to: Manually Define an Entity Data Model (Entity Framework).


The following is an example CSDL fragment for the SalesOrderDetail type in the AdventureWorks Sales Model:

The following is an example of the static CreateSalesOrderDetail method generated for the SalesOrderDetail class in AdventureWorks:

The following is an example of how to use the static CreateSalesOrderDetail method to create and save a SalesOrderDetail object:

See Also