Most of the answers on this state that the DbSet on the context must be a property. However that does not seem to be the case in this instance since the DbSet of this context is a property. Here is the sample code that I can use to recreate the problem. Please advise.
DbContext
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Dal
{
public class SampleContext : DbContext
{
public DbSet<Model.ItemCode> ItemCodes { get; set; }
public SampleContext(DbContextOptions<SampleContext> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new Mapping.ItemCodeConfiguration());
base.OnModelCreating(modelBuilder);
}
public async Task<IEnumerable<Model.ItemCode>> GetCodesByGroup(int group)
{
return await ItemCodes.Where(g => g.GroupId == group).ToListAsync();
}
}
}
Model
namespace Dal.Model
{
public class ItemCode
{
public int Id { get; set; }
public string Code { get; set; }
public string Description { get; set; }
public int Quantity { get; set; }
public int KcId { get; set; }
public int CategoryId { get; set; }
public int GroupId { get; set; }
}
}
Data Mapping
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Dal.Mapping
{
public class ItemCodeConfiguration : IEntityTypeConfiguration<Model.ItemCode>
{
public void Configure(EntityTypeBuilder<Model.ItemCode> builder)
{
builder.HasKey(b => b.Id);
builder.Property(b => b.Description)
.HasColumnName("Description")
.HasColumnType("nvarchar(255)")
.IsRequired();
builder.Property(b => b.Code)
.HasColumnName("Code")
.IsRequired();
builder.Property(b => b.KcId)
.HasColumnName("KcId");
builder.Property(b => b.Quantity)
.HasColumnName("Quantity");
builder.Property(b => b.GroupId)
.HasColumnName("GroupId")
.IsRequired();
builder.Property(b => b.CategoryId)
.HasColumnName("CategoryId")
.IsRequired();
builder.ToTable("ItemCode", "lookup");
}
}
}
Test Class
using Microsoft.EntityFrameworkCore;
using Xunit;
namespace Dal.Test
{
public class DataTest
{
readonly SampleContext context;
public DataTest()
{
var testContextOptions = new DbContextOptionsBuilder<SampleContext>();
testContextOptions.UseSqlServer(@"Server=localServerName;Database=SampleAppDb;User Id=SampleUser;Password=sampleUser!Password");
testContextOptions.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
context = new SampleContext(testContextOptions.Options);
}
[Fact]
public async void Test1()
{
var codes = await context.GetCodesByGroup(1);
Assert.NotNull(codes);
Assert.NotEmpty(codes);
}
}
}
Found the answer, was a dumb mistake. The mistake was that there are nullable int columns but the properties are not nullable. Corrected class:
namespace Dal.Model
{
public class ItemCode
{
public int Id { get; set; }
public string Code { get; set; }
public string Description { get; set; }
public int? Quantity { get; set; }
public int? KcId { get; set; }
public int CategoryId { get; set; }
public int GroupId { get; set; }
}
}