简体   繁体   English

EF Core 数据为 Null

[英]EF Core Data is Null

Most of the answers on this state that the DbSet on the context must be a property. state 上的大多数答案都认为上下文上的 DbSet 必须是一个属性。 However that does not seem to be the case in this instance since the DbSet of this context is a property.但是,在此实例中似乎并非如此,因为此上下文的 DbSet 是一个属性。 Here is the sample code that I can use to recreate the problem.这是我可以用来重现问题的示例代码。 Please advise.请指教。

Here is the code:这是代码:

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 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测试 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.错误是存在可为空的 int 列,但属性不可为空。 Corrected class:更正了 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; }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM