c# - EF Core 数据为空
问题描述
大多数关于此状态的答案都表明上下文上的 DbSet 必须是一个属性。但是,在此实例中似乎并非如此,因为此上下文的 DbSet 是一个属性。这是我可以用来重现问题的示例代码。请指教。
这是代码:
数据库上下文
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();
}
}
}
模型
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; }
}
}
数据映射
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");
}
}
}
测试班
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);
}
}
}
解决方案
找到了答案,是一个愚蠢的错误。错误是存在可为空的 int 列,但属性不可为空。修正类:
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; }
}
}
推荐阅读
- c# - 从报告参数或数据集值添加水印
- javascript - 从画廊轮播中更改大图像 onmouseover 缩略图
- xml - 松弛紧凑符号中的优先顺序是什么?
- dashboard - 节点红色仪表板 UI 不显示选项卡名称
- python - 使用 Python 查找总结数据集的唯一组合
- javascript - 如何在javascript中将类名添加到div
- r - 删除R中的单个字符
- javascript - 如何在反应中识别材料 UI 滑块?
- jquery - 如何将 JSON 数据绑定到 JQUERY Datatable 并使用 Ajax 刷新它?
- .htaccess - 如何将 Https 重定向到特定 url 的 Http