Breeze.js/NET+EF复杂的对象行为相当奇怪

Breeze.js /NET+EF complex object behaviors rather strange

本文关键字:对象 js NET+EF 复杂 Breeze      更新时间:2023-09-26

我正在开发一个自定义数据访问层,以便在bread.js中使用我所拥有的:型号:公共类产品{[键]public int ProductId{get;set;}公共字符串Upc{get;set;}公共字符串名称{get;set;}公共十进制MsrpPrice{get;set;}public int数量{get;set;}

    public virtual ICollection<ProductFeature> Features { get; set; }
    public virtual B2BCategory InB2BCategory { get; set; }
    public virtual ICollection<ImageDescriptor> Images { get; set; }
    public int CategoryId {get; set;}

}公共类ProductFeature{public int ProductId{get;set;}公共字符串名称{get;set;}公共字符串GroupName{get;set;}公共字符串操作{get;set;}公共十进制值{get;set;}}

public class ImageDescriptor
{
    public int ProductId { get; set; }
    public string Uri { get; set; }
    public DateTime Updated { get; set; }
    public bool IsDefault { get; set; }
}

上下文提供者:公共类ProductContextProvider:ContextProvider{private readonly ProductRepository repo=新的ProductRepository();

    public IQueryable<B2BCategory> Categories
    {
        get { return repo.Categories.AsQueryable(); }
    }
    public IQueryable<Product> Products
    {
        get
        {
            return repo.Products.OrderBy(p => p.ProductId).AsQueryable();
        }
    }
    protected override string BuildJsonMetadata()
    {
        var contextProvider = new EFContextProvider<ProductMetadataContext>();
        return contextProvider.Metadata();
    }
    protected override void SaveChangesCore(SaveWorkState saveWorkState)
    {…
    }
    // No DbConnections needed
    public override IDbConnection GetDbConnection()
    {
        return null;
    }
    protected override void OpenDbConnection()
    {
        // do nothing
    }
    protected override void CloseDbConnection()
    {
        // do nothing 
    }
}
internal class ProductMetadataContext : DbContext
{
    static ProductMetadataContext()
    {
        Database.SetInitializer<ProductMetadataContext>(null);
    }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new ProductFeatureConfiguration());
        modelBuilder.Configurations.Add(new ImageDescriptorConfiguration());
    }
    public DbSet<Product> Products { get; set; }
    public DbSet<B2BCategory> Categories { get; set; }
}
internal class ImageDescriptorConfiguration : EntityTypeConfiguration<ImageDescriptor>
{
    public ImageDescriptorConfiguration()
    {
    // I tried to mess up with key orders
        HasKey(i => new { i.Uri, i.ProductId});
    }
}
internal class ProductFeatureConfiguration : EntityTypeConfiguration<ProductFeature>
{
    public ProductFeatureConfiguration()
    {
        HasKey(f => new { f.ProductId, f.Name });
    }
}

我直接填充产品的特征和图像属性:

 product.Features = new Collection<ProductFeature>();
 product.Images = new Collection<ImageDescriptor>();
 …
 var imgd = new ImageDescriptor
 {
     ProductId = product.ProductId, 
     Updated = DateTime.Now, 
     Uri = defsmall, 
     IsDefault = !product.Images.Any()
 }
 product.Images.Add(imgd);
 …
 var pf = new ProductFeature
 {
    ProductId = product.ProductId,
    GroupName = "Size",
    Name = size,
    Value = size == "Small" ? new decimal(.75):size == "Medium" ? new decimal(1.3):new decimal(1.8),
    Operation = "*"
  };
  product.Features.Add(pf);

比如说,每个产品项目总共有3个产品特征和2个图像。

在客户端,我这样查询:return entityQuery.from('Products').using(EntityManager).execute();

而且…我有一件非常奇怪的事:images属性包含空数组,features属性包含5!!的数组!!!元素–ProductFeature类型的3个和ImageDescriptor类型的2个。我觉得这是个bug,你能帮我吗?

我没有看到任何代码可以创建breeze EntityManager并添加或附加新创建的实体,然后保存它们。请查看BreezeJs网站上可下载的zip中的Breeze示例。