I also wanted to do this, but I wanted to use the [DisplayName] attribute from my EF model. I could not find anyone who would have an example of this, after I found a way that I thought I would share.
First, I expanded the metadata returned from my BreezeController:
[HttpGet] public string Metadata() { // Extend metadata with extra attributes JObject metadata = JObject.Parse(contextProvider.Metadata()); string nameSpace = metadata["schema"]["namespace"].ToString(); foreach (var entityType in metadata["schema"]["entityType"]) { string typeName = entityType["name"].ToString(); Type t = Type.GetType(nameSpace + "." + typeName); foreach (var prop in t.GetProperties()) { foreach (var attr in prop.CustomAttributes) { string name = attr.GetType().Name; foreach (var p in entityType["property"]) { if (prop.Name == p["name"].ToString()) { if (attr.AttributeType.Name == "DisplayNameAttribute") { DisplayNameAttribute a = (DisplayNameAttribute)Attribute.GetCustomAttribute(prop, typeof(DisplayNameAttribute)); p["displayName"] = a.DisplayName; break; } } } } } } return metadata.ToString(); }
Then I added a little javascript after loading the metadata to pop the display names from the extended metadata where Breeze wants to find them.
manager.fetchMetadata().then(function (md) { angular.forEach(md.schema.entityType, function (et) { var etype = manager.metadataStore.getEntityType(et.name); angular.forEach(et.property, function (p) { var prop = etype.getProperty(p.name); prop.displayName = p.displayName; }); }); console.log("starting app"); angular.bootstrap($("#app"), ["app"]); });
I use angular, so if you cannot, you can ignore the angular stuff and probably get this idea. This seems to work pretty well. This should be fairly easy to extend to other attributes of the model, as well as to the regular expression validation attribute. I will probably be working on this.
FYI, some of this code is not optimized and probably can be reorganized, a little prepared, but I just got it working and thought I would share. If anyone has any suggestions for a better way, let me know. Hopefully Breeze will expand metadata in a more reliable way in the future. It looks like a hack.
jpcoder
source share