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

时间:2013-11-22 11:19:42

标签: javascript breeze

我正在开发一个在breeze.js中使用的自定义数据访问层 是)我有的: 该模型:     公共类产品     {         [键]         public int ProductId {get;组; }         public string Upc {get;组; }         public string Name {get;组; }         公共小数MsrpPrice {get;组; }         public int Quantity {get;组; }

    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;组; }         public string Name {get;组; }         public string GroupName {get;组; }         public string Operation {get;组; }         public decimal Value {get;组; }     }

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

Context Provider:   公共类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 });
    }
}

我直接填充了产品的功能和图像属性:

 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的数组! elements - ProductFeature类型的3个和ImageDescriptor类型的2个。 我想这是一个错误 - 你能帮助我吗?

1 个答案:

答案 0 :(得分:0)

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