问题
Problem
unable to locate property: Nome on entityType: Modalidade:#CreditoImobiliarioBB.Model
But this property is in the class!
No request for /odata/modalidades
is generated, only a request for /odata/$metadata
.
Code
Domain class
public class Modalidade : INamedEntity
{
public int Id { get; set; }
[StringLength(80), Required]
public string Nome { get; set; }
}
Configurations
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
var modelBuilder = new ODataConventionModelBuilder(config);
modelBuilder.EntitySet<Modalidade>("modalidades");
modelBuilder.Namespace = "CreditoImobiliarioBB.Model";
config.Routes.MapODataRoute(routeName: "odata", routePrefix: "odata", model: modelBuilder.GetEdmModel());
var jsonFormatter = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
var enumConverter = new StringEnumConverter();
jsonFormatter.SerializerSettings.Converters.Add(enumConverter);
var jqueryFormatter = config.Formatters.FirstOrDefault(x => x.GetType() == typeof(JQueryMvcFormUrlEncodedFormatter));
config.Formatters.Remove(config.Formatters.XmlFormatter);
config.Formatters.Remove(config.Formatters.FormUrlEncodedFormatter);
config.Formatters.Remove(jqueryFormatter);
config.Formatters.JsonFormatter.SerializerSettings.DateFormatHandling = DateFormatHandling.IsoDateFormat;
}
}
public static class BreezeWebApiConfig
{
public static void RegisterBreezePreStart()
{
GlobalConfiguration.Configuration.Routes.MapHttpRoute(
name: "BreezeApi"
, routeTemplate: "api/{controller}/{action}"
, defaults: new { action = "Get" }
);
}
}
CoffeScript
Defaults for breeze
breeze.NamingConvention.camelCase.setAsDefault()
breeze.config.initializeAdapterInstances
dataService: "OData"
Query
query = new breeze.EntityQuery().from("modalidades")
query = query.where("Nome", breeze.FilterQueryOp.Contains, @TermoBusca()) if @TermoBusca()
query.orderBy("Nome")
manager = new breeze.EntityManager("/odata/")
promise = manager.executeQuery(query)
.finally ->
notice.pnotify_remove()
filterUrl = creditoimobiliario.getParameterByName "\\$filter", @url
_this.TermoBusca "$filter=#{filterUrl}" if filterUrl
.fail creditoimobiliario.displayXhrNotifyError
promise.then (data, status, xhr) =>
return if not status or status != 200
ko.utils.arrayPushAll @Collection, data.value
@NextUrl data["odata.nextLink"]
promise
Metadata
<EntityType Name="Modalidade">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="Edm.Int32" Nullable="false" />
<Property Name="Nome" Type="Edm.String" Nullable="false" />
</EntityType>
回答1:
I found the answer in this link.
The solution was simply comment this line NamingConvention.camelCase.setAsDefault();
of code
回答2:
Not sure about the exact issue, but the current state of Microsoft's ODataConventionModelBuilder is that it does not YET support the full OData spec. In particular it does NOT yet support the definition of foreign keys (which Breeze needs). There may be other issues with it as well. Microsoft has claimed that this will be fixed in a later release.
For now I would simply use Microsoft's WCF data service to expose an EF model to OData. Something like:
public class ODataService : DataService<MyEFContext> {
// This method is called only once to initialize service-wide policies.
public static void InitializeService(DataServiceConfiguration config) {
// TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
// Examples:
// config.SetEntitySetAccessRule("MyEntityset", EntitySetRights.All);
...
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
config.UseVerboseErrors = true;
}
来源:https://stackoverflow.com/questions/17529608/unable-to-locate-property-nome-on-entitytype