2013-11-22 22 views
1

我正在開發一個自定義的數據訪問層在breeze.js 消耗我有什麼: 模型: 公共類產品 { [重點] public int ProductId {get;組; } public string Upc {get;組; } public string Name {get;組; } public decimal MsrpPrice {get;組; } public int Quantity {get;組; }Breeze.js/NET + EF複雜對象的行爲很奇怪

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

上下文提供: 公共類ProductContextProvider:ContextProvider { 私人只讀ProductRepository回購=新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();

而且......我得到了很奇怪的東西: 圖像屬性包含一個空數組,features元素包含一個5的數組!元素 - 3類型ProductFeature和2類型ImageDescriptor。 我認爲這是一個錯誤 - 請問您能幫助我嗎?

回答

0

我沒有看到任何創建微風EntityManager的代碼,並添加或附加您新創建的實體,然後保存它們。請從BreezeJs網站下載的zip文件中查看Breeze示例。