45

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.

ryudice
  • 36,476
  • 32
  • 115
  • 163
  • 3
    Using partial classes is the correct way to do this, and works fine for me. You need to show your code, your mappings, and the exact error, as what you've described thus far already works. – Craig Stuntz Jan 04 '10 at 14:03

4 Answers4

128

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);
     }
 }
IronAces
  • 1,857
  • 1
  • 27
  • 36
Kniganapolke
  • 5,073
  • 5
  • 22
  • 19
  • this is a good solution when you want to add properties to the entity object but don't want them associated with the database or Mapping file – mknopf Jun 28 '13 at 19:52
  • 1
    Using the [NotMapped] attribute is the easiest way to go if you have a property you need on all entites such as EntityState that you declare on a base class. No need to edit every configuration class and remember to do so every time a new entity is introduced. – Mike Devenney Jan 10 '17 at 14:38
  • Hey, thanks for your answer. But when I try it, i get : "Does not support untyped value in non-open type." What am I doing wrong ? – Toodoo Feb 27 '18 at 09:38
  • 1
    @Toodoo, the error seems to be related to OData not to EF. – Kniganapolke Feb 28 '18 at 12:38
  • Because it was written years later after the OP stopped caring about this topic. – ShadowKras Mar 01 '19 at 20:46
11

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.

Mark
  • 3,123
  • 4
  • 20
  • 31
Orion Adrian
  • 19,053
  • 13
  • 51
  • 67
2

This page really helped me. I'll add exactly what I added to my mapping configuration after seeing Kniganapolke's answer.

 public TheObjectName()
   {
       this.HasKey(t => t.ID);
       this.Ignore(t => t.IsProcess); //we don't want EF to worry about this
    }

Thanks everyone, thanks SO!

BroTrevor
  • 41
  • 5
1

I'm seriously late to the conversation, but you also want to mark the partial as serializable and the property as serializable - if you ever plan to JSON or serialize the objects:

[Serializable()] 
public partial class MyClass {
    private System.Nullable<int> _Age;
    [global::System.Runtime.Serialization.DataMemberAttribute(Order = 4)] 
    public System.Nullable<int> Age {
            ...
    }
}

Both the [Serializable()] and the [global:] directives are needed. If you excluded the [global:], any time you serialized it, it'd be ignored and not included in the serialization.

Jason
  • 3,020
  • 2
  • 24
  • 23
  • 1
    Excellent! I was spinning my wheels for so long trying to figure out why only one of my posted variables was always null. This did the trick! Thanks! – RobDigital Sep 11 '15 at 22:06