Breeze: 'A nonnullable DataProperty cannot have a null defaultValue. Name: undefined'

StackOverflow https://stackoverflow.com/questions/16694322

  •  30-05-2022
  •  | 
  •  

the error depends on a field of type geography in my SQL Server db. This field is not nullable and has a default property. I tried to add JsonIgnore attribute to this field, but it is still present in metadata and I have this error: Metadata import failed for breeze/BreezeMyPlace/Metadata; Unable to process returned metadata:A nonnullable DataProperty cannot have a null defaultValue. Name: undefined. I'm using nuget packages "Breeze for ASP.NET Web API projects v. 1.3.3", Entity Framework 5.0, JSon.NET 5.0.5, etc. How can I prevent serialization of this field? Thanks

有帮助吗?

解决方案

The [JsonIgnore] attribute tells the JSON formatter to ignore the property during serialization but it is still part of the EF model and it still appears in the metadata generated from the DbContext. Serialization and Metadata are distinct concerns.

You can hide it from metadata by adding the EDM [NotMapped] attribute to the property in your model.

If you don't want to touch your model, you can tell EF to ignore it via Fluent Configuration in your DbContext, e.g,

modelBuilder.Entity<Customer>().Ignore(t => t.CustomerID_OLD);

If you need that property to be accessible in server logic but hidden from Breeze metadata, you can create a dedicated MetadataDbContext that inherits from your working DbContext and put the "ignore" code in there. See the NorthwindMetadataContext in DocCode for an example.

Reminder: In general, you can only hide nullable properties from EF. This will work in your case because the non-nullable property has a default value defined in the database schema. Otherwise, the database would reject inserts.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top