2016-05-17 6 views
-1

私はVS2012を使用してC#で顧客に連絡を試みるのを追跡するWebアプリケーションを作成しています。私は接触試行タイプを保存する2つのrefテーブルと、試着結果を保存しています。これらは常に固定値なので、試行結果が保存されます。私が抱えている問題は、DBからApp_ContactAttemptを取得すると、接続されたRef_ContactOutcomeおよびRef_ContactTypeエンティティなしでApp_ContactAttemptエンティティを返すことになります。レイジーローディングを有効にし、コンテキストファイルでプロキシの作成を有効にして、すべてのrefテーブルのプロパティをvirtualに設定します。しかし、私は、データベースからApp_ContactAttemptを取得すると、refテーブルが添付されていません。誰でも私が何ができるか考えていますか?より多くの情報が必要な場合は、私はそれを提供することができます。Cant 'が関連するエンティティを読み込むためにEF6を取得する

UPDATE 右、私はこのようになりますApp_ContactAttempt、取得するためのサービスのセットアップがあります。

public App_ContactAttempt GetContactAttempt(int contactAttemptId) 
    { 
     using (var logger = new MethodLogger(contactAttemptId)) 
     { 
      var contactAttempt = new App_ContactAttempt(); 
      try 
      { 
       contactAttempt = _unitOfWork.ContactAttempts.Get(contactAttemptId); 
      } 
      catch (Exception e) 
      { 
       logger.LogException(e.InnerException); 
      } 
      return contactAttempt; 
     } 
    } 

私はこのサービスを利用する場合、私はサービスを呼び出すときに、私はApp_ContactAttemptを取り戻すが、Ref_ContactTypeをRef_ContactOutcomeはnullです。しかし、私はそうのようなDBのコンテキストを使用して、コントローラ内からDBに呼び出すとき:

var db = new ParsDatabaseContext(); 

    var contactAttemptTest1 = _clientService.GetContactAttempt(contactAttempt.ContactAttemptId); 

    var contactAttemptTest2 = db.App_ContactAttempt.Where(x => x.ContactAttemptId == contactAttempt.ContactAttemptId); 

contactAttemptTest1はRef_ContactTypeとRef_ContactOutcome両方ともヌルを有するApp_ContactAttemptを返します。ただし、ContactAttemptTest2は、Ref_ContactTypeとRef_ContactOutcomeの両方が設定されたApp_ContactAttemptを返します。

Context.cs:彼らはすべてで助ける場合ここでは2 は、コンテキストとクラスである

UPDATE ...これは私が手掛かりを持っていないので、私の問題を絞り込むことができますホープ

public partial class ParsDatabaseContext : DbContext { public ParsDatabaseContext() : base("name=ParsDatabaseContext") { this.Configuration.LazyLoadingEnabled = true; this.Configuration.ProxyCreationEnabled = true; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { throw new UnintentionalCodeFirstException(); } public DbSet<App_Client> App_Client { get; set; } public DbSet<App_ContactAttempt> App_ContactAttempt { get; set; } public DbSet<Ref_ContactOutcome> Ref_ContactOutcome { get; set; } public DbSet<Ref_ContactType> Ref_ContactType { get; set; } public virtual ObjectResult<GetClient_Result> GetClient(Nullable<int> clientID) { var clientIDParameter = clientID.HasValue ? new ObjectParameter("ClientID", clientID) : new ObjectParameter("ClientID", typeof(int)); return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<GetClient_Result>("GetClient", clientIDParameter); } } 

App_ContactAttempt.cs

public partial class App_ContactAttempt 
{ 
    public int ContactAttemptId { get; set; } 
    public int ClientId { get; set; } 
    public Nullable<System.DateTime> ContactDate { get; set; } 
    public Nullable<int> ContactType { get; set; } 
    public Nullable<int> ContactOutcome { get; set; } 
    public string Notes { get; set; } 

    public virtual Ref_ContactOutcome Ref_ContactOutcome { get; set; } 
    public virtual Ref_ContactType Ref_ContactType { get; set; } 
} 

Ref_ContactOutcome.cs

public partial class Ref_ContactOutcome 
{ 
    public Ref_ContactOutcome() 
    { 
     this.App_ContactAttempt = new HashSet<App_ContactAttempt>(); 
    } 

    public int ContactOutcomeId { get; set; } 
    public string Description { get; set; } 

    public virtual ICollection<App_ContactAttempt> App_ContactAttempt { get; set; } 
} 

Ref_ContactType.cs

public partial class Ref_ContactType 
{ 
    public Ref_ContactType() 
    { 
     this.App_ContactAttempt = new HashSet<App_ContactAttempt>(); 
    } 

    public int ContactTypeId { get; set; } 
    public string Description { get; set; } 

    public virtual ICollection<App_ContactAttempt> App_ContactAttempt { get; set; } 
} 
+0

「参照テーブルがありません」 - どういう意味ですか?デバッグ中は、 'Ref_ContactOutcome'プロパティが表示され、nullまたは何ですか? –

+0

リポジトリパターンに 'Include'拡張子を使用するか、遅延読み込みを有効にする必要があります。 – Igor

+1

遅延ロードを機能させるには、*からエンティティをロードしたコンテキスト*が、ナビゲーションプロパティにアクセスするまでに利用可能(かつ、処理されていない)でなければなりません。非常に短いコンテキストで作業したい場合は、熱心な読み込み(例: 'Include')を使用してください。 – Jcl

答えて

2

遅延ロードは、プロキシクラスの作成に使用されたDBContextが使用可能な場合にのみ機能します。あなたのケースでは、タイプApp_ContactAttemptの代理オブジェクトcontactAttemptを処理するために使用されたDBContextが既に廃棄されているため、プロキシが切り離されています。また

ていることを確認してください:オブジェクトがプロキシである場合は、プロキシエンティティがDBContextに接続されているかどうかを確認するために

public static bool IsProxy(object type) 
{ 
    return type != null && ObjectContext.GetObjectType(type.GetType()) != type.GetType(); 
} 

https://msdn.microsoft.com/en-us/library/ee835846%28v=vs.100%29.aspx

this answerを参照してください。あなたがチェックすることができ

dbContext.Configuration.ProxyCreationEnabled = true; 

そして、 。

あなたは別の既存のコンテキストに既存のデタッチのエンティティを添付して、もう一度それを遅延ロードすることができます:あなたが既に知っているデータベースに存在しますが、 実体を持っている場合は

db.App_ContactAttempts.Attach(contactAttemptTest1); 

が、現在ではありませんコンテキストによって追跡されている場合は、 にDbSetのAttachメソッドを使用してエンティティを追跡するように指示できます。コンテキスト内の エンティティはUnchanged状態になります。

hereを参照してください。だからあなたの例では

using (var db = new ParsDatabaseContext()) 
{ 
    var contactAttemptTest1 = _clientService.GetContactAttempt(contactAttempt.ContactAttemptId);  
    db.App_ContactAttempts.Attach(contactAttemptTest1);  
    Debug.Print(contactAttemptTest1.Ref_ContactType.Description); 
} 

が動作するはずです。

0

使用が含まれます。例えば

var contactAttemps = db.App_ContactAttempts 
         .Includes("Ref_ContactOutcome") 
         .Includes("Ref_ContactTypes") 
         .ToList(); 
+0

私はデータベースからエンティティを取得するためにリポジトリパターンを使用しています。私はそのレコードを返すContactAttemptIdを渡すサービス設定がありますが、Ref_ContactOutcomeとRef_ContactTypesの両方がnullです。 – necrofish666

-1

あなたは、エンティティ自体またはDTO(データ転送オブジェクト)を返すされていますか?

DTOを返す場合は、マッピングが正しく行われていることを確認してください。

あなたのエンティティオブジェクトを投稿してください。

関連する問題