Search code examples
entity-frameworkpoco

entity framework insert only some columns


I have a POCO class

public class Main
{
    public int ExstraColumn{ get; set; }
}
public class User : Main
{
    [Key]
    public int Id { get; set; }

    public int? Age { get; set; }


}

public class News : Main
{
    [Key]
    public int Id { get; set; }

    public int? ReadCount { get; set; }
}

now i want entity framework inserts only age column in user. But it gives invalid column name ExstraColumn

how to tell entity framework that ExstraColumn field is only special usage?


Solution

  • You can use the [NotMapped] attribute in the System.ComponentModel.DataAnnotations namespace.

    http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.schema.notmappedattribute(v=vs.110).aspx

    Denotes that a property or class should be excluded from database mapping.