I want to create a custom property on one of my entities mapped from the database, however this property is not mapped to the database, I created the property using partial classes but when trying to compile I get an error telling me that the property is not mapped. Is there an attribute or something I should add? Thanks in advance.
The NotMapped attribute is used to specify that an entity or property is not to be mapped to a table or column in the database.
A navigation property is an optional property on an entity type that allows for navigation from one end of an association to the other end. Unlike other properties, navigation properties do not carry data. A navigation property definition includes the following: A name. (Required)
Shadow properties are useful when there's data in the database that shouldn't be exposed on the mapped entity types. Indexer properties are entity type properties, which are backed by an indexer in . NET entity class.
You can also mark your property with [NotMapped]
attribute or use Ignore
method from fluent API.
Property
public class EntityName { [NotMapped] private string PropertyName { get; } }
Fluent API
public class Entities : DbContext { public DbSet<EntityType> Users { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { // Some database configuration modelBuilder.Entity<EntityType>() .Ignore(i => i.PropertyName); } }
Use partial classes to add the properties or methods you want added. E.g.
namespace WhateverNamespaceYourEntityModelIsIn { public partial class TheNameOfYourEntity { public string MyNewProperty { get; set; } } }
and that should do you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With