This documentation is archived and is not being maintained.

How to: Create an Object Using the Static Create Method

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.

<Property Name="CarrierTrackingNumber" Type="String" MaxLength="25" Unicode="true" FixedLength="false" 
          a:SetterAccess="Protected" xmlns:a="" />

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:

<EntityType Name="SalesOrderDetail">
    <PropertyRef Name="SalesOrderID" />
    <PropertyRef Name="SalesOrderDetailID" />
  <Property Name="SalesOrderID" Type="Int32" Nullable="false" />
  <Property Name="SalesOrderDetailID" Type="Int32" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
  <Property Name="CarrierTrackingNumber" Type="String" MaxLength="25" Unicode="true" FixedLength="false" 
            a:SetterAccess="Protected" xmlns:a="" />
  <Property Name="OrderQty" Type="Int16" Nullable="false" />
  <Property Name="ProductID" Type="Int32" Nullable="false" />
  <Property Name="SpecialOfferID" Type="Int32" Nullable="false" />
  <Property Name="UnitPrice" Type="Decimal" Nullable="false" Precision="19" Scale="4" />
  <Property Name="UnitPriceDiscount" Type="Decimal" Nullable="false" Precision="19" Scale="4" />
  <Property Name="LineTotal" Type="Decimal" Nullable="false" Precision="38" Scale="6" annotation:StoreGeneratedPattern="Computed" />
  <Property Name="rowguid" Type="Guid" Nullable="false" />
  <Property Name="ModifiedDate" Type="DateTime" Nullable="false" />
  <NavigationProperty Name="SalesOrderHeader" Relationship="AdventureWorksModel.FK_SalesOrderDetail_SalesOrderHeader_SalesOrderID" FromRole="SalesOrderDetail" ToRole="SalesOrderHeader" />

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

public static SalesOrderDetail CreateSalesOrderDetail(global::System.Int32 salesOrderID, global::System.Int32 salesOrderDetailID, global::System.Int16 orderQty, global::System.Int32 productID, global::System.Int32 specialOfferID, global::System.Decimal unitPrice, global::System.Decimal unitPriceDiscount, global::System.Decimal lineTotal, global::System.Guid rowguid, global::System.DateTime modifiedDate)
    SalesOrderDetail salesOrderDetail = new SalesOrderDetail();
    salesOrderDetail.SalesOrderID = salesOrderID;
    salesOrderDetail.SalesOrderDetailID = salesOrderDetailID;
    salesOrderDetail.OrderQty = orderQty;
    salesOrderDetail.ProductID = productID;
    salesOrderDetail.SpecialOfferID = specialOfferID;
    salesOrderDetail.UnitPrice = unitPrice;
    salesOrderDetail.UnitPriceDiscount = unitPriceDiscount;
    salesOrderDetail.LineTotal = lineTotal;
    salesOrderDetail.rowguid = rowguid;
    salesOrderDetail.ModifiedDate = modifiedDate;
    return salesOrderDetail;

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

int orderId = 43680;
using (AdventureWorksEntities context
    = new AdventureWorksEntities())
    var order = (from o in context.SalesOrderHeaders
                 where o.SalesOrderID == orderId
                 select o).First();

    // Add a new item.
    SalesOrderDetail newItem = SalesOrderDetail.CreateSalesOrderDetail(
        0, 0, 5, 711, 1, (decimal)13.0368,
        0, 0, Guid.NewGuid(), DateTime.Now);


See Also