私はEntity Frameworkを初めて使用し、一部のレガシーデータアクセスコードをEFを使用するように変換することを調査しています。EFで次のことが可能かどうか、可能であればその方法を知りたいです。このような Customer テーブルがあるとします
CustomerId | ProductId | StartDate | EndDate
--------------------------------------------
100 | 999 | 01/01/2012| null
また、製品オブジェクトのキャッシュとして、別の場所 (XML ファイルなど) から製品データを読み込むとします。
public class Customer
{
public int CustomerId {get;set;}
public int Product {get;set}
public DateTime StartDate {get;set;}
public DateTime? EndDate {get;set;}
}
public class Product
{
public int ProductId {get;set;}
public int Description {get;set}
}
現在、CustomerDal クラスでは、メソッドは StoredProc を使用して、このような Customer オブジェクトを取得します
Customer GetCustomer(int customerId)
{
// setup connection, command, parameters for SP, loop over datareader
Customer customer = new Customer();
customer.CustomerId = rdr.GetInt32(0);
int productId = rdr.GetInt32(1);
// ProductCache is a singleton object that has been initialised before
customer.Product = ProductCache.Instance.GetProduct(productId);
customer.StartDate = rdr.GetDateTime(2);
customer.EndDate = rdr.IsDbNull(3) ? (DateTime?)null : rdr.GetDateTime(3);
return customer;
}
私の質問は、DB からではなく別の方法で、この場合はメモリ内キャッシュから Product プロパティを設定する Customer オブジェクトを具体化するときに EF を使用してこれが可能であるということです。同様に、新しい Customer オブジェクトを保存すると、Products プロパティから ProductId のみが取得され、値が DB に保存されます。