2017-03-07 6 views
1

は、Entity Framework Model First Class Libraryのデータを呼び出すClass Libraryの機能を呼び出しています。Configuration.ProxyCreationEnabled = false場所はありますか?

//------------------------------------------------------------------------------ 
// <auto-generated> 
//  This code was generated from a template. 
// 
//  Manual changes to this file may cause unexpected behavior in your application. 
//  Manual changes to this file will be overwritten if the code is regenerated. 
// </auto-generated> 
//------------------------------------------------------------------------------ 

namespace SADatastore 
{ 
using System; 
using System.Data.Entity; 
using System.Data.Entity.Infrastructure; 

public partial class SADatastoreEntities : DbContext 
{ 
    public SADatastoreEntities() 
     : base("name=SADatastoreEntities") 
    { 
    } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     throw new UnintentionalCodeFirstException(); 
    } 

    public virtual DbSet<DB> DBS { get; set; } 
    .... 
    } 
} 

が、私はその後、Connections.csファイルIユーザへのを持っている:私は恐ろしい苦しんでい

だから、私のDataStoreクラスライブラリで、私はautogeneratedコンテキストを持っている「自己参照ループが検出されました」ヨーヨーとして...

public partial class SADatastoreEntities 
{ 
    public SADatastoreEntities(string connStr) 
     : base(string.Format(@"metadata=res://*/SAModel.csdl|res://*/SAModel.ssdl|res://*/SAModel.msl;provider=System.Data.SqlClient;provider connection string='{0}'", connStr)) 
    { 
     // http://stackoverflow.com/questions/12868996/permanently-disable-configuration-proxycreationenabled-in-ef 
     Configuration.ProxyCreationEnabled = false; 

    } 
} 

contextconnection stringを渡します私は上記のコードで私はを設定しようとしている私は私の問題を解決する必要があります読んでいることがわかります。しかし、それは効果がありません。

public static List<SA_Items> SA_GetAllItemDetails(string connStr, DateTime? lastUpdated, int limit, int offset) { 
     using (SADatastoreEntities db = new SADatastoreEntities(connStr)) { 
      if (lastUpdated == null) 
       lastUpdated = new DateTime(1900, 1, 1); 

      return db.SA_Items 
       .Include("SA_Model") 
       .Include("SA_Model.SA_Manufacturers") 
       .Where(x => x.LastUpdated > lastUpdated) 
       .OrderBy(x => x.ItemID) 
       .Skip(offset) 
       .Take(limit) 
       .ToList(); 
     } 
    } 

私はそうのJSONオブジェクトを文字列に変換しようとしていますラインを追加しました:私はここでは、次のコントローラ

public class SA_GetAllItemDetailsController : ApiController 
{ 
    [Route("SA_GetAllItemDetails")] 
    [HttpGet] 
    public List<SA_Items> Get(DateTime? lastUpdated = null, int limit = 100, int offset = 0) 
    { 
     try 
     { 
      if (Helpers.Authorise(Request, "WriteVisitors").Authorised) 
      { 

       List<SA_Items> result = new List<SA_Items>(); 

       result = SADatastoreEngine.Functions.Item.SA_GetAllItemDetails(
        Settings.Default.ConnectionString, 
        lastUpdated, 
        limit, 
        offset 
        ); 

       // conver the result to a string to have a look at it 
       string json = JsonConvert.SerializeObject(result, Formatting.Indented); 

       return result; 
      } 
      else 
      { 
       throw new HttpResponseException(Helpers.RespondNotAuthorised()); 
      } 
     } 
     catch (Exception e) 
     { 
      throw new HttpResponseException(Helpers.RespondWithAnError(e)); 
     } 
    } 
} 
public class SA_GetItemByItemIdController : ApiController 
{ 
    [Route("SA_GetItemByItemId/{id}")] 
    [HttpGet] 
    public SA_Items Get(string id) 
    { 
     try 
     { 
      if (Helpers.Authorise(Request, "WriteVisitors").Authorised) 
       return SADatastoreEngine.Functions.Item.GetItemByItemId(Settings.Default.ConnectionString, id); 
      else 
      { 
       throw new HttpResponseException(Helpers.RespondNotAuthorised()); 
      } 
     } 
     catch (Exception e) 
     { 
      throw new HttpResponseException(Helpers.RespondWithAnError(e)); 
     } 
    } 
} 
public class SA_CreateItemController : ApiController 
{ 
    [Route("SA_CreateItem")] 
    [HttpPost] 
    [ValidateViewModel] 
    public Dictionary<string, string> Post(SA_Items item) 
    { 
     try 
     { 
      // get the auth details 
      AuthorisationModel auth = Helpers.Authorise(Request, "WriteVisitors"); 

      if (Helpers.Authorise(Request, "WriteVisitors").Authorised) 
      { 
       return SADatastoreEngine.Functions.Item.CreateItem(Settings.Default.ConnectionString, auth.DBSId, item); 
      } 
      else 
      { 
       throw new HttpResponseException(Helpers.RespondNotAuthorised()); 
      } 
     } 
     catch (Exception e) 
     { 
      throw new HttpResponseException(Helpers.RespondWithAnError(e)); 
     } 
    } 
} 
public class SA_UpdateItemController : ApiController 
{ 
    [Route("SA_UpdateItem")] 
    [HttpPut] 
    public Dictionary<string, string> Post(SA_Items item) 
    { 
     try 
     { 
      // get the auth details 
      AuthorisationModel auth = Helpers.Authorise(Request, "WriteVisitors"); 

      if (auth.Authorised) 
      { 
       return SADatastoreEngine.Functions.Item.UpdateItem(Settings.Default.ConnectionString, auth.DBSId, item); 
      } 
      else 
      { 
       throw new HttpResponseException(Helpers.RespondNotAuthorised()); 
      } 
     } 
     catch (Exception e) 
     { 
      throw new HttpResponseException(Helpers.RespondWithAnError(e)); 
     } 
    } 
} 

SA_GetAllItemDetails機能である持っている私のAPIで

私はそれを見ることができます。しかし、これがエラーを投げる場所です。それは間違った場所にありますので、私は設定

Configuration.ProxyCreationEnabled = false; 

を感じている

が無視されています。しかし、私はそれがどこにあるべきかを考え出すことはできません。

私が間違ってやっていることを誰にでも見てもらえますか?

JsonSerializerSettings _jsonSettings = new JsonSerializerSettings() 
{ 
     ReferenceLoopHandling = ReferenceLoopHandling.Ignore 
}; 

// convert the result to a string to have a look at it 
string json = JsonConvert.SerializeObject(result, Formatting.Indented, _jsonSettings); 

「System.OutOfMemoryExceptionには、」エラー

+0

SADatastoreEngine.Functions.Item.SA_GetAllItemDetailsの内容は何ですか? – Evk

+0

@Evkはそのコードを追加しました...ごめんなさい –

+0

'SA_GetAllItemDetails'はこの問題を抱えませんが、' public SADatastoreEntities():base( "name = SADatastoreEntities") 'を使用すると、間違った動作をします。あなたが 'this(" name = ... "と呼ぶことを意図していたと思う")' –

答えて

0

ここでの問題スローされた種類の例外につながる:私は今、それをシリアル化しようとする私のコードを変更した

EDIT

Includeにあります。 SA_Itemsを検索すると、SA_Modelが含まれており、これはおそらくSA_Itemに戻ります。そのSA_Itemはすでにコンテキストで追跡されているため(クエリを作成します)、遅延参照を使用する必要はなく、その参照を埋めるためにプロキシを作成する必要はありません。結果として、SA_ItemはすべてSA_Modelへの参照を持ち、LazyLoadingEnabledまたはProxyCreationEnabled(参照はすでにコンテキストによって追跡されているため)に関係なく、SA_Itemの参照を返します。

その逆参照のロードを防止することはできません。したがって、直列化時に手動でループをチェックする必要があります。

+0

私の知らないことには申し訳ありません。「私がNewtonsoft.Jsonを使っているときに、手動でループをチェックする必要がありますか?」というのはどういう意味ですか? –

+0

@TrevorDaniel、例えばReferenceLoopHandling.Ignore:http://www.newtonsoft。 com/json/help/html/ReferenceLoopHandlingIgnore.htm – Evk

+0

私のAPIには既に_config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;がありません。これは効果がありません:( –

関連する問題