Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a non mapped property in an entity (entity framework)

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.

like image 393
ryudice Avatar asked Jan 01 '10 04:01

ryudice


People also ask

What is NotMapped in Entity Framework?

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.

What is Navigation property in Entity Framework?

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)

What are the Shadow and indexer properties in Entity Framework?

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.


2 Answers

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);      }  } 
like image 83
Kniganapolke Avatar answered Oct 10 '22 16:10

Kniganapolke


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.

like image 39
Orion Adrian Avatar answered Oct 10 '22 17:10

Orion Adrian