2017-01-16 15 views
2

私はこのようになります2つのナビゲーションプロパティを持つPOCOクラスを持っている:Entity Frameworkの、ストアドプロシージャおよびナビゲーションプロパティ

public class Center : Archive 
{ 
    public int Id { get; set; } 
    [MaxLength(50)] public string ExternalId { get; set; } 
    [Required, MaxLength(150)] public string Name { get; set; } 
    [MaxLength(255)] public string Description { get; set; } 
    [MaxLength(50)] public string Address1 { get; set; } 
    [MaxLength(50)] public string Address2 { get; set; } 
    [MaxLength(50)] public string Address3 { get; set; } 
    [MaxLength(50)] public string Address4 { get; set; } 
    [MaxLength(10)] public string PostCode { get; set; } 

    [MaxLength(100)] public string CollectionPointContact { get; set; } 
    [MaxLength(50)] public string CollectionPointTelephone { get; set; } 
    [MaxLength(50)] public string CollectionPointFax { get; set; } 
    [MaxLength(255)] public string CollectionPointEmail { get; set; } 

    public int CompanyId { get; set; } 
    public Company Company { get; set; } 
    public IList<Collection> Collections { get; set; } 
} 

public class Company : Archive 
{ 
    public int Id { get; set; } 
    [Required, MaxLength(100)] 
    public string Name { get; set; } 
    public string Logo { get; set; } 
    [Required, MaxLength(50)] 
    public string Theme { get; set; } 

    public IList<User> Members { get; set; } 
    public IList<Center> Centers { get; set; } 
} 

public class Collection : Archive 
{ 
    public int Id { get; set; } 
    public int CenterId { get; set; } 
    [Required, MaxLength(50)] public string Reference { get; set; } 
    [MaxLength(255)] public string Description { get; set; } 
    [MaxLength(50)] public string CustomerReference { get; set; } 
    [MaxLength(100)] public string CustomerName { get; set; } 
    [MaxLength(100)] public string CustomerBusinessName { get; set; } 
    [MaxLength(100)] public string SupplierName { get; set; } 

    [Column(TypeName="Date")] public DateTime PlannedCollectionDate { get; set; } 
    public DateTime DeliveredDate { get; set; } 
    public DateTime CollectedDate { get; set; } 

    [MaxLength(100)] public string ReceivedBy { get; set; } 
    public string ReceivedBySignature { get; set; } 

    [MaxLength(100)] public string CollectedBy { get; set; } 
    public string CollectedBySignature { get; set; } 

    public CollectionStatus Status { get; set; } 
} 

最初は、私はプロパティを設定するために、デフォルト遅延ロードを使用していたが、それはでした受け入れられないデータの300行(プロパティを含む)をロードするには2秒以上かかります。

私は熱心な読み込みで、生成されたSQLが結合を使用することがわかっているので、熱心な読み込みに切り替えることにしました。これはそうではありませんでした。まだ完了するのに2秒くらいかかりました。

私の次の課題は、ストアドプロシージャを試してみることでした。これは(まだ私はまだ過大だと思う1Sで)速いと思えますが

internal class Repository<T> : IDisposable, IRepository<T> where T : class 
{ 
    private readonly DatabaseContext _context; 
    private readonly DbSet<T> _dbEntitySet; 

    public Repository(DatabaseContext context) 
    { 
     if (context == null) throw new ArgumentNullException("context"); 
     _context = context; 
     _dbEntitySet = context.Set<T>(); 
    } 

    public IList<T> ExecuteStoredProcedure(string storedProcedureName, IList<SqlParameter> parameters = null) 
    { 
     if (parameters == null) 
      return _context.Database.SqlQuery<T>($"exec { storedProcedureName }").ToList(); 
     else 
      return _context.Database.SqlQuery<T>($"exec { storedProcedureName } @{ parameters[0].ParameterName }").ToList(); 
    } 
} 

、それは私のナビゲーションプロパティに引っ張っていません。私のリポジトリのクラスでは、私は一般的な方法を書きました。

私はhow to do it if you are not using genericsを説明している記事を見つけましたが、どのナビゲーションプロパティでもできます。

私はリフレクションを使用することを考えましたが、それはオーバーヘッドコストを追加するだけです。

誰もがこの問題に遭遇して問題を解決したのですか、それとももっと良い方法がありますか?

+1

最初にデータをロードした方法のコードがないと、パフォーマンスが向上したかどうかを判断する方法がありません。私は非常にめったにそれが一般的にパフォーマンスの悪夢であるように怠惰なローディングをお勧めします。 EFがクエリを生成していないため、ストアドプロシージャを呼び出すとナビゲーションプロパティが自動入力されることはありません。あなたはsprocの道を続行したい場合は私に知らせてください(私は実際には、私はEFは私のためにperformantクエリを生成していないので、私はprocに使用していた非常に高度なクエリを切り替えた)。 –

+0

あなたの[前の質問](http://stackoverflow.com/q/41664805/861716)では、あなたは非常に少量のデータを扱っていると言いました。まず、基礎が堅実でない場合、最適化できないものを最適化する前に、まず索引の適切な使用法を分析する必要があります。 SQL Server Management Studioを使用して、「実際のクエリプランを含める」を使用してクエリを実行し、インデックスヒントを探します。 *次に、本当に必要なデータについて考えてみてください。あなたは本当に完全なレコードをクライアントに持ってくる必要がありますか? *予測*はパフォーマンスを大幅に向上させる傾向があります。 –

答えて

-1

怠惰なサポートのために仮想プロパティを使用して熱心な負荷を組み合わせて、IQeryableのIncludeメソッドを熱心な負荷関連のエンティティに呼び出すことができます。トラッキングクエリを使用しないで、パフォーマンスを向上させることができますhttps://msdn.microsoft.com/en-us/data/jj556203(変更はefで検出されません)

関連する問題