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" 
          DefaultValue="1A-2B-3C"
          a:SetterAccess="Protected" xmlns:a="https://schemas.microsoft.com/ado/2006/04/codegeneration" />

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).

Example

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

<EntityType Name="SalesOrderDetail">
  <Key>
    <PropertyRef Name="SalesOrderID" />
    <PropertyRef Name="SalesOrderDetailID" />
  </Key>
  <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" 
            DefaultValue="1A-2B-3C"
            a:SetterAccess="Protected" xmlns:a="https://schemas.microsoft.com/ado/2006/04/codegeneration" />
  <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" />
</EntityType>

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

Public Shared Function CreateSalesOrderDetail(ByVal salesOrderID As Global.System.Int32, ByVal salesOrderDetailID As Global.System.Int32, ByVal orderQty As Global.System.Int16, ByVal productID As Global.System.Int32, ByVal specialOfferID As Global.System.Int32, ByVal unitPrice As Global.System.Decimal, ByVal unitPriceDiscount As Global.System.Decimal, ByVal lineTotal As Global.System.Decimal, ByVal rowguid As Global.System.Guid, ByVal modifiedDate As Global.System.DateTime) As SalesOrderDetail
    Dim salesOrderDetail As 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
End Function
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:

Dim orderId As Integer = 43680
Using context As New AdventureWorksEntities()
    Dim order = (From o In context.SalesOrderHeaders
        Where o.SalesOrderID = orderId
        Select o).First()

    ' Add a new item. 
    Dim newItem As SalesOrderDetail = SalesOrderDetail.CreateSalesOrderDetail(0, 0, 5, 711, 1, CDec(13.0368), _
    0, 0, Guid.NewGuid(), DateTime.Now)
    order.SalesOrderDetails.Add(newItem)

    context.SaveChanges()
End Using
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);
    order.SalesOrderDetails.Add(newItem);

    context.SaveChanges();
}

See Also

Concepts

Creating, Adding, Modifying, and Deleting Objects
Working with Objects