あなたの具体的な答えは、単に特定のコードブロックのためだけ無効プロキシの作成に答える
が、あなたはそうのような熱心な負荷にあなたのエンティティを持つことになります。そのインスタンスのみに影響します
try
{
DbContext.Configuration.ProxyCreationEnabled = false;
var userWithRoles = DbContext.Users.Include(x => x.Roles).Where(x => x.Id == id).FirstOrDefault();
}
finally
{
DbContext.Configuration.ProxyCreationEnabled = true;
}
...。エンティティの読み込み中に例外が発生した場合、オプションが元に戻されるようにすることができるので、try finallyブロックでこれをラップしました。
また、あなたのDbContext
コンストラクタ内でこれをグローバルに設定することができますが、私はこれをお勧めしません:
public class YourDbContext : DbContext
{
public YourDbContext() : base("name=ConnectionString")
{
this.Configuration.ProxyCreationEnabled = true;
}
}
私の推薦は、APIへのあなたのデータベース・エンティティの公開を回避することです。
// original entities
public class User {
public int Id{get; set;}
public string Name{get; set;}
public ICollection<Role> Roles{get; set;}
// other fields like birthdate, status, password, etc...
}
public class Role {
public int Id{get; set;}
public string Name{get; set;}
public virtual User User{get; set;}
}
// DTO classes, keeping only the fields you want
// original entities
public class UserDTO {
public int Id{get; set;}
public string Name{get; set;}
public ICollection<RoleDTO> Roles{get; set;}
}
public class RoleDTO {
public int Id{get; set;}
public string Name{get; set;}
}
// project them like this:
var userDTO = DbContext.Users.Where(x => x.Id == id)
.Select(u => new UserDTO
{
Id = u.Id,
Name = u.Name,
Roles = u.Roles.Select(r => new RoleDTO
{
Id = r.Id,
Name = r.Name
}),
})
.FirstOrDefault();
DTOクラスを作成して公開することもできます。 AutoMapper
のようなDTOクラスのプロジェクトをより簡単できれいにするツールがありますが、これは単なる例です。
私は興味がありますが、なぜプロキシオブジェクトではないのですか?とにかく「役割」の子孫です。 – Alisson
私はJSONとしてユーザーを送る必要があります。 'Ok(user)'はjsonにプロキシオブジェクトを追加しないでください。(それはあります) – bluray