私はEFコア(既存のデータベースからのデータベースの最初の設計)に2つの関連エンティティを持ち、1対多の関係をロードする際に問題があります。これはwebapi ASP.NETコア1.0アプリケーションです
ブランドエンティティ
[Table("tblBranding")]
public class Brand {
[Key]
[Column("brandingId")]
public int BrandId { get; set; }
[Column("BrandingActive")]
public bool Active { get; set; }
[JsonIgnore]
[Column("DeadBrand")]
public bool DeadBrand { get; set; }
[Column("BrandingSiteTitle")]
public string Name { get; set; }
//navigation properties
public virtual ICollection<Event> Events { get; set; }
}
イベントエンティティ:
[Table("tblEvents")]
public class Event
{
public int EventId { get; set; }
[Column("eventActive")]
public bool Active { get; set; }
[Column("eventName")]
public string Name { get; set; }
public DateTime EventCloseDate {get;set;}
public int PaxAllocationLimit { get; set; }
//navigation properties
[Column("BrandingId")]
public int BrandId { get; set; }
public virtual Brand Brand { get; set; }
public virtual ICollection<Session> Sessions { get; set; }
}
DbContextのOnModelCreatingのFLUID APIのコード:
modelBuilder.Entity<Event>()
.HasOne(e => e.Brand)
.WithMany(b => b.Events).HasForeignKey(e=>e.BrandId);
public virtual DbSet<Brand> Brands { get; set; }
public virtual DbSet<Event> Events { get; set; }
BrandsControllerのコード:
[HttpGet]
public IActionResult Get() {
//var brands = from b in _context.Brands
// where b.Active == true
// orderby b.BrandName
// select b;
var brands = _context.Brands.Include(e => e.Events).Where(b => b.Active == true).OrderBy(b => b.Name);
return new ObjectResult(brands);
}
EventsControllerのコード
// GET: api/values
[HttpGet("{id:int?}")]
public IActionResult Get(int? id) {
var events = from e in _context.Events
where e.Active == true
orderby e.Name
select e;
if (!events.Any()) {
return HttpNotFound();
}
if (id != null) {
events = events.Where(e => e.EventId == id).OrderBy(e => 0);
if (events.Count() == 0) { return HttpNotFound(); }
return new ObjectResult(events);
}
else {
return new ObjectResult(events);
}
}
APIを使用してブランドを読み込もうとすると、例外が発生します。
Microsoft.Data.Entity.Storage.Internal.RelationalCommandBuilderFactory:情報:実行されたDbCommand(80ms)[Parameters = []、CommandType = 'Text'、CommandTimeout = '30 '] SELECT [t]。[EventId]、[t]。 [EventIdive]、[t]。[EventIdive]、[t]。[EventId1]、[t]。[イベント名]、[t]。[PaxAllocationLimit] FROM [tblEvents] AS [t] ] [ブランディングID] FROM [tblBranding] AS [e] WHERE [e]。[BrandingActive] = 1)AS [e] ON [t] Microsoft.Data.Entity.Query.Internal.SqlServerQueryCompilationContextFactory:エラー:データベース内で例外が発生しましたが、データベース内で例外が発生しましたが、[BrandingId] = [e] [brandingId] ORDER BY [クエリの結果System.Data.SqlClient.SqlException(0x80131904):無効な列名 'EventId1'です。
それ以外に、 "Includes"を使わずに関連するエンティティを読み込む方法はありますか? includeを使用せずに標準のLINQクエリを使用すると、関連するエンティティはNULLとしてロードされます
更新
私は今無効な列のエラーを取得しています - 私は私の前のコードで気づいた私はブランドのICollectionでバーチャルを使用していなかった
今私はなぜSQLでEventId1列を生成しているのかわかりません
EF 7バージョンは1.0.0-rc1-finalです
UPDATE-2上記のコードと全く同じコードが与えられたコードで例外を循環依存関係例外に変更したコードを再生した後、無効な列名を以前に生成していた理由(EventId1)
私自身の質問に答えて - それを考え出した -
ここで使用されている2つのエンティティは、EF 7で完全に定義された関係を使用していますが、JSONシリアライザはそうではありません。これは、循環依存関係を作成します(Brand Contains Events List)
そのため、ここでの解決策は、[JsonIgnore]属性を子の関係プロパティに追加することでした
更新されたイベントクラス:
[Table("tblEvents")]
public class Event
{
public int EventId { get; set; }
[Column("eventActive")]
public bool Active { get; set; }
[Column("eventName")]
public string Name { get; set; }
public DateTime EventCloseDate {get;set;}
public int PaxAllocationLimit { get; set; }
//navigation properties
[JsonIgnore]
[Column("brandingId")]
public virtual int BrandId { get; set; }
[JsonIgnore]
public virtual Brand Brand { get; set; }
//public virtual ICollection<Session> Sessions { get; set; }
}