【问题标题】:The seed entity for entity type 'Publication' cannot be added because there was no value provided for the required property 'Image'无法添加实体类型“Publication”的种子实体,因为没有为所需属性“Image”提供值
【发布时间】:2020-06-24 18:23:53
【问题描述】:

我正在尝试测试我的应用程序,但出现以下错误: The seed entity for entity type 'Publication' cannot be added because there was no value provided for the required property 'Image'

问题是实际提供了值。

我正在尝试播种一些数据以进行这种测试:

public static class NewsierContextFactory
    {
        public static NewsierContext Create()
        {
            var options = new DbContextOptionsBuilder<NewsierContext>()
                .UseInMemoryDatabase(Guid.NewGuid().ToString())
                .Options;

            var dateTimeMock = new Mock<IDateTime>();
            dateTimeMock.Setup(m => m.Now)
                .Returns(new DateTime(3001, 1, 1));

            var context = new NewsierContext(options, dateTimeMock.Object);

            context.Database.EnsureCreated();

            context.Publishers.AddRange(
                  new Publisher()
                  {
                      Id = "one",
                      Image = "default-user.png",
                      Name = "Name",
                      Surname = "Surname",
                      Email = "test@email.com",
                      Password = "publisher",
                      Role = "admin",
                      CreatedAt = DateTime.Now,
                      LastModifiedAt = DateTime.Now
                  }
              );

            context.Categories.AddRange(
                new Category()
                {
                    Id = "one",
                    Name = "categ",
                    CreatedAt = DateTime.Now,
                    LastModifiedAt = DateTime.Now
                }
            );

            context.Publications.AddRange(
                new Publication
                {
                    Id = "one",
                    Image = "default-publication.png",
                    Title = "title",
                    Value = "value",
                    Views = 37,
                    CategoryId = "one",
                    PublisherId = "one",
                    CreatedAt = DateTime.Now,
                    LastModifiedAt = DateTime.Now
                }
            );

            context.SaveChanges();

            return context;
        }

        public static void Destroy(NewsierContext context)
        {
            context.Database.EnsureDeleted();

            context.Dispose();
        }
    }

如您所见,我的 Publisher 实体具有确切的值“Image”。而且他们都对这个值有相同的配置。

public class PublisherConfiguration : IEntityTypeConfiguration<Publisher>
    {
        public void Configure(EntityTypeBuilder<Publisher> builder)
        {
            builder.Property(p => p.Image)
                .HasMaxLength(256)
                .HasDefaultValue("Static/Images/default-user.png")
                .IsRequired();

            builder.Property(p => p.Name)
                .HasMaxLength(32)
                .IsRequired();

            builder.Property(p => p.Surname)
                .HasMaxLength(64)
                .IsRequired();

            builder.Property(p => p.Email)
                .HasMaxLength(128)
                .IsRequired();

            builder.Property(p => p.Password)
                .HasMaxLength(256)
                .IsRequired();

            builder.Property(p => p.Role)
                .HasMaxLength(12)
                .HasDefaultValue("publisher")
                .IsRequired();

            builder.HasData(
                new Publisher
                {
                    Id = "publisher-one",
                    Name = "name",
                    Surname = "sur",
                    Email = "admin@newsier.com",
                    Password = "admin",
                    Role = "admin",
                    CreatedAt = DateTime.Now
                },
                new Publisher
                {
                    Id = "publisher-two",
                    Name = "name",
                    Surname = "sur",
                    Email = "admin@email.com",
                    Password = "publisher",
                    Role = "publisher",
                    CreatedAt = DateTime.Now
                }
            );
        }
    }
public class PublicationConfiguration : IEntityTypeConfiguration<Publication>
    {
        public void Configure(EntityTypeBuilder<Publication> builder)
        {
            builder.Property(p => p.Image)
               .HasDefaultValue("default-publication.png")
               .IsRequired();

            builder.Property(p => p.Title)
                .HasMaxLength(256)
                .IsRequired();

            builder.Property(p => p.Value)
                .IsRequired();

            builder.Property(p => p.PublisherId)
                .IsRequired();

            builder.Property(p => p.CategoryId)
                .IsRequired();

            builder.HasOne(p => p.Category)
                .WithMany(categ => categ.Publications)
                .OnDelete(DeleteBehavior.Restrict);

            builder.HasOne(p => p.Publisher)
                .WithMany(pub => pub.Publications)
                .OnDelete(DeleteBehavior.Restrict);

            builder.HasData(
                new Publication {
                    Id = "publication-one",
                    Title = "the first publication",
                    Value = "the content of the very first publication on the web-site",
                    CreatedAt = DateTime.Now,
                    CategoryId = "category-one",
                    PublisherId = "publisher-one"
                },
                new Publication
                {
                    Id = "publication-two",
                    Title = "the second publication",
                    Value = "the content of the second publication on the web-site",
                    CreatedAt = DateTime.Now,
                    CategoryId = "category-one",
                    PublisherId = "publisher-two"
                }
            );
        }
    }

这是我的实体:

public class Publisher : BaseEntity
    {
        public Publisher()
        {
            Publications = new HashSet<Publication>();
        }

        public string Image { get; set; }
        public string Name { get; set; }
        public string Surname { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
        public string Role { get; set; }

        public ICollection<Publication> Publications { get; set; }
    }
public class Publication : BaseEntity
    {
        public string Image { get; set; }
        public string Title { get; set; }
        public string Value { get; set; }
        public long Views { get; set; }

        public string PublisherId { get; set; }
        public Publisher Publisher { get; set; }

        public string CategoryId { get; set; }
        public Category Category { get; set; }
    }

还有基础实体:

public class BaseEntity
    {
        public string Id { get; set; }
        public DateTime CreatedAt { get; set; }
        public DateTime LastModifiedAt { get; set; }
    }

我尝试从 Publication 中删除“Image”属性,错误消失了,但 Publisher 中存在相同的“Image”值并且它没有提供任何错误。

我该如何解决这个问题以及为什么这对 Publicher 实体来说不是问题?

还有一点值得一提的是,迁移工作得很好。这意味着我可以使用“HasData”添加一些数据,但不能使用“Add”或“AddRange”。

【问题讨论】:

    标签: c# database asp.net-core entity-framework-core asp.net-core-webapi


    【解决方案1】:

    所以,我认为您正在寻找的答案在这里:Seed values must be different from default values

    Publisher 工作的原因是种子值和ImageHasData 值不同。 Publication 等同于 default-publication.png 的值,显然这让播种者认为你没有设置它?

    是的,这也让我挑眉。

    【讨论】:

    • 很好的答案,真的,但它不起作用。我更改了默认值并尝试使用另一个值进行添加。此外,我每次进行更改时都会重建我的项目,但它无论如何都不起作用。 Image 属性的默认值为“default-publication.png”,为了好玩,我使用“not-the-same-value-as-default”值尝试在“AddRange”的帮助下添加出版物。不过我有同样的错误,我想也许你的答案就是解决方案,但还有另一个问题,你怎么看?
    • 没关系,我意识到了问题所在。尽管有默认值,我应该使用HasDataImage 提供一个值。我认为我不应该这样做,因为我有一个默认值。您的回答帮助我意识到这一点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-26
    • 1970-01-01
    • 2022-08-17
    • 1970-01-01
    相关资源
    最近更新 更多