Search code examples
c#.netentity-frameworkentity-framework-5dbmigrate

Stop entity framework from creating computed column in db migration


Thanks in advance, I have been using code first with EF-DB migration in my project.

The challenge I am having is now I want to add a property in my Class which should only be long to the class and not Create a database table-column. I have tried setting scaffolding off and even setting the column as computed column. However when I run the Add-Migration "XYZ" it keeps creating that column.

//Class

    [ScaffoldColumn(false)]
    [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Computed)]
    public Nullable<int> Counter { get; set; }

// Migration

  public override void Up()
  {
       AddColumn("dbo.Registers", "Counter", c => c.Int());
  }

Solution

  • If you don't want Entity Framework to generate a database column for something, you can add the [NotMapped] attribute to that property, and it will be ignored.