I’m trying to use MongoDB.EntityFrameworkCore 7 preview.1. Should the BsonElement attribute work on an entity model? I have my Entity model designed with standard pascal case property names and I have an existing MongoDb collection. I’m getting an error on trying to query an item in the collection. “Element ‘Address’ not found.” If I change all my entity properties to lower case it works.
[BsonElement("name")]
public string Name { get; set; } = string.Empty;
[BsonElement("address")]
public PostalAddress Address { get; set; }
Does the BsonElement work in the 7.0.0-preview.1 version?
Welcome to the MongoDB Community Forums and thank you for trying EFCore 7 preview.1. Preview 1 does not contain a convention to read BSON attributes, but we are considering adding this for release. At the moment, you can either match the casing of property names between your models and the database or you can override them using the model builder. For example, you can override DbContext.OnModelCreating in your derived DbContext class similar to the following:
Another quick point to note if you prefer attributes over the code-first model builder approach. You can use the EF-provided [Column("address"]. Although these are technically elements in the BSON document and not columns, EF likes to call things tables and columns due to its relational heritage. We do have a [Collection("collName")] synonym to [Table("tableName")] but we haven’t implemented the [Element("elemName")] equivalent. We may simply enable use of our BSON attributes such as [BsonElement("elemName")] in the next preview.