我在过滤查询时遇到问题。这是我的查询:
var products = _context.Products.Select(x => new GetProductsDto {
Id = x.Id,
Name = x.Name.Value,
Quantity = x.Quantity.Value,
Brand = x.Brand,
Displayed = x.Displayed,
Price = x.Price,
Category = x.Category.Name.Value
});
products = products.Where(p => p.Name.Contains(request.SearchKey));
这是我的产品实体:
public class Product{
public Name Name { get; private set; }
public Quantity Quantity { get; private set; }
public string Brand { get; private set; }
public string Description { get; private set; }
public int Price { get; private set; }
public bool Displayed { get; private set; }
public int CategoryId { get; set; }
public virtual Category Category { get; set; }
public virtual ICollection<ProductImage> ProductImages { get; private set; }
public virtual ICollection<ProductFeature> ProductFeatures { get; private set; }
}
My Name ValueObject:
public class Name : ValueObject
{
public const byte MaxLen = 100;
public string Value { get; private set; }
private Name()
{
}
public static Name Create(string name)
{
var shopName = new Name();
if (name.Length > MaxLen)
{
shopName.Result.WithError(
string.Format(Validations.MaxLenValidation, DataDictionary.ShopName, MaxLen));
return shopName;
}
shopName.Value = name;
return shopName;
}
}
这是我的产品实体Map到数据库:
internal class ProductEntityMap : IEntityTypeConfiguration<Product>
{
public void Configure(EntityTypeBuilder<Product> builder)
{
builder.HasKey( x => x.Id);
builder.Property(x => x.Id)
.ValueGeneratedOnAdd();
builder.Property(x => x.Quantity)
.HasConversion(x => x.Value,
x => Domain.Aggregates.Products.ValueObjects.Quantity.Create(x));
builder.Property(x => x.Name)
.HasConversion(x => x.Value,
x => Domain.SharedKernel.Name.Create(x));
}
}
我认为错误是因为我在实体中使用了'Name' valueObject。请帮我
1条答案
按热度按时间guykilcj1#
最后我找到了解决办法。我将这一行添加到我的ValueObject类中,例如在我的Name ValueObject类中,它是这样的:
我改变了我的查询如下:
最后,我检查了SQL Server Profiler工具,以确保查询在服务器而不是客户端执行。