1
votes

I’m developing a custom data access layer to be consumed in breeze.js What I have: The Model: public class Product { [Key] public int ProductId { get; set; } public string Upc { get; set; } public string Name { get; set; } public decimal MsrpPrice { get; set; } public int Quantity { 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;}

} public class ProductFeature { public int ProductId { get; set; } public string Name { get; set; } public string GroupName { get; set; } public string Operation { get; set; } public decimal Value { 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; }
}

The Context Provider: public class ProductContextProvider : ContextProvider { private readonly ProductRepository repo =new 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 });
    }
}

I’m stuffing the Features and Images properties of a Product directly:

 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);

Totally there are, say, 3 product features and 2 images per product item.

In the client side I query this like: return entityQuery.from('Products').using(EntityManager).execute();

And… I’ve got the very strange thing: The images property contains an empty array, the features property contains an array of 5!!! elements – 3 of type ProductFeature and 2 of type ImageDescriptor. I think this is a bug – could you help me, please?

1

1 Answers

0
votes

I don't see any code that creates a breeze EntityManager and adds or attaches your newly created entities and then saves them. Please take a look at the Breeze examples in the downloadable zip from the BreezeJs website.