sto cercando di elencare alcune categorie (relazione genitore-figlio) e non ho il seguente comando per elencare solo le categorie genitoreEF Codice primo confronto tra i valori null genera strana interrogazione
context.Categories.Where(c => c.ParentId == null)
Ma la query SQL generato da EF non restituisce nulla di query
SQL generato da EF
SELECT
CAST(NULL AS int) AS [C1],
CAST(NULL AS varchar(1)) AS [C2],
CAST(NULL AS bit) AS [C3],
CAST(NULL AS int) AS [C4]
FROM (SELECT 1 AS X) AS [SingleRowTable1]
WHERE 1 = 0
categoria modello
public class Category
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsActive { get; set; }
public virtual IList<Category> SubCategories { get; set; }
internal int? ParentId { get; set; }
public virtual Category Parent { get; set; }
public override bool Equals(object obj)
{
var categoryToCompare = obj as Category;
if (categoryToCompare == null) return false;
return categoryToCompare.Id == Id;
}
public override int GetHashCode()
{
return Id.GetHashCode();
}
}
Mapping
public class CategoryConfiguration : EntityTypeConfiguration<Category>
{
public CategoryConfiguration()
{
ToTable("tbl_category");
HasKey(c => c.Id);
Property(c => c.Id).HasColumnName("cd_category");
Property(c => c.Name).HasColumnName("ds_category");
Property(c => c.IsActive).HasColumnName("fl_active");
Property(c => c.ParentId).HasColumnName("cd_base_category").IsOptional();
HasMany(c => c.SubCategories).WithRequired(c => c.Parent).HasForeignKey(c => c.ParentId);
}
}
@caerolus sì EF generato che –
Usa '(c =>! C.ParentId.HasValue)' – Fendy
@Fendy genera la stessa query –