静的 Create メソッドを使用してオブジェクトを作成する方法 (Entity Framework)

Entity Framework ツールは、概念スキーマ定義言語 (CSDL) を使用して、オブジェクト レイヤーを定義するコードを生成します。 データ クラスの生成時、それぞれのクラスは、静的な create ファクトリ メソッドを使用して生成されます。 このメソッドは、オブジェクトをインスタンス化し、クラスの null に設定できないすべてのプロパティを設定します。 このメソッドには、CSDL 内で Nullable="false" 属性が適用され、モデルで既定値が定義されておらず、Entity Framework がアクセスできる、すべてのプロパティのパラメーターが含まれています。 次の例は、概念モデルのプロパティに既定値および set アクセサーを指定する方法を示しています。

<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" />

概念モデルのこのプロパティ定義を使用して、コード ジェネレーターは、既定値 "1A-2B-3C" と protected set アクセサーが指定された CarrierTrackingNumber プロパティがあるエンティティを生成します。

このメソッドは、多くの必須プロパティを持つオブジェクトを作成する場合に使用します。

このトピックの例には、Adventure Works Sales Model が使用されています。このトピックのコードを実行するには、あらかじめプロジェクトに Adventure Works Sales Model を追加し、Entity Framework を使用するようにプロジェクトを構成しておく必要があります。詳細については、「Entity Data Model ウィザードを使用する方法 (Entity Framework)」、または「Entity Framework プロジェクトを手動で構成する方法」、および「Entity Data Model を手動で定義する方法 (Entity Framework)」を参照してください。

AdventureWorks Sales ModelSalesOrderDetail 型の CSDL の例を次に示します。

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

AdventureWorks の SalesOrderDetail クラスに対して生成された静的な CreateSalesOrderDetail メソッドの例を次に示します。

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;
}

静的な CreateSalesOrderDetail メソッドを使用して SalesOrderDetail オブジェクトを作成および保存する例を次に示します。

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();
}

参照

概念

オブジェクトの作成、追加、変更、および削除 (Entity Framework)
オブジェクトの使用 (Entity Framework)