Usuário com melhor resposta
Mapeamento de duas tabelas e select em ambas.

Pergunta
-
Boa tarde, Srs.
Fiz um mapeamento manual em duas tabelas:
Content
[Table("VLZ_Content")] public class Content : DbContext { [Key] public int IdContent { get; set; } [Column("Title")] public string Title { get; set; } [Column("Description")] public string Description { get; set; } [Column("CreatedBy")] public int CreatedBy { get; set; } [Column("IdFile")] public int? IdFile { get; set; } public virtual File File { get; set; } }
File
[Table("VLZ_File")] public class File : DbContext { [Key] public int IdFile { get; set; } [Column("Name")] public string Name { get; set; } [Column("Title")] public string Title { get; set; } }
Agora estou tentando fazer um select em Content que me traga o conteúdo de File também.
public Content GetContentByID(int IdContent) { var result = _context.Contents.Where(c => c.File == _context.Files && c.IdContent == IdContent).FirstOrDefault(); return result; }
Se eu fizer o select somente em Content ele retorna o objeto correto, porém, com o objeto File vazio.
Como posso pegar todo conteúdo em um único select?
Vlw!
Danilo Oliveira www.coffeeandcodes.com.br
Respostas
-
Consegui resolver o problema mapeando o objeto da seguinte forma:
HasRequired(a => a.ContentType).WithMany(b => b.Contents).HasForeignKey(c => c.IdContentType); HasRequired(a => a.User).WithMany(b => b.Contents).HasForeignKey(c => c.CreatedBy); HasOptional(a => a.File).WithMany(b => b.Contents).HasForeignKey(c => c.IdFile); HasOptional(a => a.PartnerCategory).WithMany(b => b.Contents).HasForeignKey(c => c.IdPartnerCategory);
E colocando o atributo reverso nos objetos que fazem ligação com Content
public virtual ICollection<Content> Contents { get; set; }
Funcionou direitinho.
Obrigado.
Danilo Oliveira www.coffeeandcodes.com.br
- Marcado como Resposta DaniloTec quarta-feira, 29 de abril de 2015 18:17
Todas as Respostas
-
Tente usando LINQ, duas formas:
var result = (from a in _context.Contents join b in _context.File on a.idFile equals b.idFile select new { Contet = a, File = b }).FirstOrDefault(); var result = (from a in _context.Contents.Include("File") select a).FirstOrDefault();
-
Eu consegui resolver em partes usando o Fluent API. O problema é que agora ele não trás todos os objetos que herdam do objeto Content.
Objeto Content:
public class Content { public int IdContent { get; set; } public string Title { get; set; } public string Description { get; set; } public string Link { get; set; } public string Address { get; set; } public string Button { get; set; } public string RegulationLink { get; set; } public string Discount { get; set; } public int? Sequence { get; set; } public bool? Highlighted { get; set; } public int CreatedBy { get; set; } public int? UpdatedBy { get; set; } public DateTime? ExpireAt { get; set; } public virtual File File { get; set; } public virtual ContentType ContentType { get; set; } public virtual PartnerCategory PartnerCategory { get; set; } }
ContentMap:
public class ContentMap : EntityTypeConfiguration<Content> { public ContentMap() { //Key HasKey(t => t.IdContent); //Fields Property(t => t.IdContent).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); Property(t => t.Title); Property(t => t.Description); Property(t => t.Link); Property(t => t.Address); Property(t => t.Button); Property(t => t.RegulationLink); Property(t => t.Discount); Property(t => t.Sequence); Property(t => t.Highlighted); Property(t => t.CreatedBy); Property(t => t.UpdatedBy); Property(t => t.ExpireAt); //Table ToTable("VLZ_Content"); //Relationship HasOptional(t => t.File).WithOptionalDependent(u => u.Content).Map(m => m.MapKey("IdFile")); HasOptional(t => t.PartnerCategory).WithOptionalDependent(u => u.Content).Map(m => m.MapKey("IdPartnerCategory")); HasRequired(t => t.ContentType).WithRequiredDependent(u => u.Content).Map(m => m.MapKey("IdContentType")); } }
O objeto ContentType é obrigatório (one-to-one), mas quando popula o ContentType dentro do Content ele só trás o primeiro objeto e não retorna os demais:
Repare que na coluna Conteúdo ele só trás o primeiro registro preenchido, os demais que repetem não.
Danilo Oliveira www.coffeeandcodes.com.br
-
-
-
Consegui resolver o problema mapeando o objeto da seguinte forma:
HasRequired(a => a.ContentType).WithMany(b => b.Contents).HasForeignKey(c => c.IdContentType); HasRequired(a => a.User).WithMany(b => b.Contents).HasForeignKey(c => c.CreatedBy); HasOptional(a => a.File).WithMany(b => b.Contents).HasForeignKey(c => c.IdFile); HasOptional(a => a.PartnerCategory).WithMany(b => b.Contents).HasForeignKey(c => c.IdPartnerCategory);
E colocando o atributo reverso nos objetos que fazem ligação com Content
public virtual ICollection<Content> Contents { get; set; }
Funcionou direitinho.
Obrigado.
Danilo Oliveira www.coffeeandcodes.com.br
- Marcado como Resposta DaniloTec quarta-feira, 29 de abril de 2015 18:17