【发布时间】: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