Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6236819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:54:46+00:00 2026-05-24T10:54:46+00:00

I am using Entity Framework Code First in my ASP.NET MVC application. One of

  • 0

I am using Entity Framework Code First in my ASP.NET MVC application. One of my classes has several columns that are added together. I am storing these columns as computed columns in the tables by running an alter table script in the database initializer. Let’s say the class looks like:

public class Bond
{
        public decimal ParAmountOfIssuance { get; set; }
        public decimal AccruedInterest { get; set; }
        public decimal Premium { get; set; }
        public decimal OriginalIssueDiscount { get; set; }
}

The alter script is something like:

alter table Bonds
add TotalSources as (ParAmountOfIssuance + AccruedInterest + Premium - OriginalIssueDiscount)

I want the Total Sources column to be available for viewing in the web app. What’s the best way to accomplish this? The [DatabaseGenerated(DatabaseGeneratedOption.Computed)] attribute doesn’t work because EF Code First creates the table from the class before the alter script is ran.

Any suggestions are welcome.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T10:54:47+00:00Added an answer on May 24, 2026 at 10:54 am

    I have a somewhat of an workaround.

    You can only use calculated field on a existing database.

    If you add your property to CF object as:

    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public decimal TotalSources { get; set; }
    

    and if you add a line in your script that will delete information about generation of that database:

    DELETE FROM [dbo].[EdmMetadata]
    

    CF will assume it is existing database and it will work, I have just tried.

    UPDATE I forgot, if you add property to your Bond entity like this, then in your script you need to alter it to make it calculated, not add it 🙂
    You can even manually “synchronize” database and model – at point where you have everything working without this field, add it in model as computed, and in table as calculated. When you delete hash from edm metadata table CF will work without trying to regenerate model with database.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing a web application using ASP.NET MVC 3 and Entity Framework Code First
I am using Entity Framework 4.1 code first and ASP.NET MVC 3 and I
I'm building an ASP.NET MVC 3 site using the code-first Entity Framework 4 approach.
I'm writing an ASP.net MVC3 application using Entity Framework Code First and SqlCe4. I
I am using Entity Framework code first and ASP.NET MVC 3. I have the
I am using Entity Framework 4.1 code first and ASP.NET MVC 3. I have
I'm using ASP.NET MVC 3 and Entity Framework 4.1 Code First. Let's say I
I am using ASP.NET MVC 3 and Entity Framework 4.1 code first . I
First some brief background: I have an existing ASP.NET MVC 1 application using Entity
I am attempting to add Entity Framework, code first, to an MVC application that's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.