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

entity-framework

Question

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.

1
37
1/1/2010 4:06:27 AM

Accepted Answer

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.

10
5/30/2014 1:37:17 PM

Popular Answer

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);
     }
 }


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow