具有未映射属性的Breeze控制器元数据

时间:2015-01-14 13:46:52

标签: c# entity-framework breeze asp.net-web-api2

我创建了一个未映射到我的数据库的属性。我在数据库层中使用了Ignore方法:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{        
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

    modelBuilder.Entity<Category>().Ignore(c => c.Translations);
    modelBuilder.Entity<Manufacturer>().Ignore(c => c.Translations);
    modelBuilder.Entity<Product>().Ignore(c => c.Translations);
    modelBuilder.Entity<SpecificationAttribute>().Ignore(c => c.Translations);
    modelBuilder.Entity<SpecificationAttributeOption>().Ignore(c => c.Translations);

    base.OnModelCreating(modelBuilder);

}

这是我的班级/实体的一个例子:

public class SpecificationAttribute
{      

    private ICollection<Translation> _translations;

    public int Id { get; set; }
    public string Name { get; set; }

    public virtual ICollection<Translation> Translations
    {
        get { return _translations ?? (_translations = new List<Translation>()); }
        set { _translations = value; }
    }        

}

public class Translation : BaseEntity
{
    public int EntityId { get; set; }
    public int LanguageId { get; set; }
    public string KeyGroup { get; set; } 
    public string Key { get; set; } 
    public string Value { get; set; }        
}

我只想在Breeze客户端上使用 Translations 属性,而不是在我的数据库中保存/更改任何内容。因此,当我调用Breeze方法时,我创建了一段填充 Translations 属性的代码。

[HttpGet]
[Route("SpecificationAttributes")]
public IQueryable<SpecificationAttribute> SpecificationAttributes()
{
    var collection = _contextProvider.Context.SpecificationAttributes;
    collection.ForEach(x => x.Translations = _contextProvider.Context.Translations.Where(y => y.EntityId == x.Id && y.KeyGroup == typeof(SpecificationAttribute).Name).ToList());
    return collection;
}

当我手动调用此方法时,一切正常,结果包括翻译。但是当我在我的角度应用程序上使用breeze客户端调用此函数时,不包括翻译。有谁知道问题可能是什么?我已经尝试过使用expand命令,但后来出现了错误。

1 个答案:

答案 0 :(得分:0)

我已经解决了在beeze客户端上扩展属性的问题。

代码 - entityManagerFactory.js

(function() {
    'use strict';

    angular.module('app').factory("entityManagerFactory", entityManagerFactory);
    entityManagerFactory.$inject = ["config", "model"];

    function entityManagerFactory(config, model) {
        breeze.config.initializeAdapterInstance('modelLibrary', 'backingStore', true);
        //breeze.NamingConvention.camelCase.setAsDefault();

        // Tell breeze not to validate when we attach a newly created entity to any manager.
        // We could also set this per entityManager
        new breeze.ValidationOptions({ validateOnAttach: false }).setAsDefault();

        var serviceName = config.remoteApiUrl + "/breeze/";
        var metadataStore = createMetadataStore();

        var provider = {
            metadataStore: metadataStore,
            newManager: newManager
        };

        return provider;

        function createMetadataStore() {
            var store = new breeze.MetadataStore();
   ------>  model.configureMetadataStore(store);
            return store;
        }

        function newManager() {
            var mgr = new breeze.EntityManager({
                serviceName: serviceName,
                metadataStore: metadataStore,
                saveOptions: new breeze.SaveOptions({ allowConcurrentSaves: false })
            });
            return mgr;
        }
    }
})();

一段代码 - model.js

function configureMetadataStore(metadataStore) {       
    registerSpecificationAttribute(metadataStore);
    registerSpecificationAttributeOption(metadataStore);
}

function registerSpecificationAttribute(metadataStore) {

    function SpecificationAttribute() {
        this.Translations = [];
    }

    metadataStore.registerEntityTypeCtor("SpecificationAttribute", SpecificationAttribute);

}

function registerSpecificationAttributeOption(metadataStore) {

    function SpecificationAttributeOption() {
        this.Translations = [];
    }

    metadataStore.registerEntityTypeCtor("SpecificationAttributeOption", SpecificationAttributeOption);

}