This is my DBContext
modelBuilder.Entity<Solicitud>(entity =>
{
entity.Property(e => e.Id).ValueGeneratedNever();
entity.HasOne(d => d.IdComentariosNavigation)
.WithMany(p => p.Solicitud)
.HasForeignKey(d => d.Id)
.OnDelete(DeleteBehavior.ClientSetNull);
});
modelBuilder.Entity<SolicitudRevisionComentarios>(entity =>
{
entity.Property(e => e.id).ValueGeneratedNever();
entity.Property(e => e.id_solicitud)
.IsRequired()
.HasMaxLength(4)
.IsUnicode(false);
entity.Property(e => e.motivo)
.IsRequired()
.HasMaxLength(300)
.IsUnicode(false);
});
this is my controller
var result = new PagedCollectionResponse<SolicitudViewModel>();
IEnumerable<Solicitud> items;
items = _context.Solicitud.Include(p => p.IdEstatusNavigation).Include(p => p.IdComentariosNavigation).Where(p => p.Id == filterModel.Id);
And this is my models
public partial class Solicitud : IAuditableEntity
{
public int Id { get; set; }
public virtual SolicitudRevisionComentarios IdComentariosNavigation { get; set; }
}
public class SolicitudRevisionComentarios
{
public int id { get; set; }
public int id_solicitud { get; set; }
public string motivo { get; set; }
}
The sql query I want to do is the next
SELECT [p].*, [p.Comentarios].*, [p.Estatus].*
FROM [Solicitud] AS [p]
INNER JOIN [CatalogoEstatus] AS [p.Estatus] ON [p].[IdEstatus] = [p.Estatus].[Id]
LEFT JOIN [SolicitudRevisionComentarios] AS [p.Comentarios] ON [p].[Id] = [p.Comentarios].[id]
WHERE [p].[Id] = 666 --this is an example
But instead, the next query appears
SELECT [p].*, [p.Comentarios].*, [p.Estatus].*
FROM [Solicitud] AS [p]
INNER JOIN [CatalogoEstatus] AS [p.Estatus] ON [p].[IdEstatus] = [p.Estatus].[Id]
INNER JOIN [SolicitudRevisionComentarios] AS [p.Comentarios] ON [p].[Id] = [p.Comentarios].[id]
WHERE [p].[Id] = 0 --this is an example
SolicitudRevisionComentarios
is used when there is a value related to another table according to the id. If these id does not exist in SolicitudRevisionComentarios
, return null. But i don´t all return to null query
This is an ASP.NET Entity Framework Core WebService with Microsoft .NET Core 2.2
The issue is that you are expecting a 1 to 0/1 relationship between Solictud and Comments, however your schema is 1 to many. What in your schema restricts more than one of your SolictudRevisionCommentario records having the same id_Solictud value?
For a Solictud to have only 1 Comment, it would need to form a many-to-1 relationship or a 1-to-0/1.
Many-to-1 would have a null-able SolictudRevisionCommentario ID on Solicitud.
1-to-0/1 would see id_Solictud to be the PK in the comment table.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.