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

  • SEARCH
  • Home
  • 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 8727507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:26:47+00:00 2026-06-13T08:26:47+00:00

I am using Entity Framework mapped to my database. I have a Basket model

  • 0

I am using Entity Framework mapped to my database. I have a Basket model which can have many BasketItem models, and I have Promotions and Coupons models.
This is for eCommerce checkout functionality and I just don’t understand how this will work, here goes:

Because my BasketItems have a foreign key relationship to the Basket if I want to sum up the Subtotal for my basket items in a partial class, I can do this:

        public decimal Subtotal {
        get {
            return this.BasketItems.Sum(pb => pb.Subtotal);
            }
        }

This is helpful because I can use this inside a view, there’s no mucking around with passing a DB context through and it’s DRY, etc. etc.

Now I want to apply promotions or coupons to my Subtotal ideally I want it to look like this:

        public decimal DiscountedSubtotal {
        get { 
            decimal promotions_discount = 0;
            decimal coupons_discount = 0;
            return Subtotal - promotions_discount - coupons_discount;
            }
        }

But there is no access to Promotions or Coupons without either creating some crazy and unnecessary relationships in my database or some light hacking to get this functionality to work. I don’t know what I should do to overcome this problem.

Solution 1:

        public decimal DiscountedSubtotal(DatabaseEntities db) {
        decimal promotions_discount = from p in db.Promotions
                                  select p.Amount;
        decimal coupons_discount = from c in db.Coupons
                               select c.Amount;
        return Subtotal - promotions_discount - coupons_discount;
        }

I don’t want to use this in my View pages, plus I have to send through my context every time I want to use it.

Solution 2: (untested)

    public List<Promotion> Promotions { get; set; }
    public List<Coupon> Coupons { get; set; }

    public Basket()
        : base() {
            DatabaseEntities db = new DatabaseEntities();
            Promotions = db.Promotions.ToList();
            Coupons = db.Coupons.ToList();
    }

A bit of light hacking could provide me with references to promotions and coupons but i’ve had problems with creating new contexts before and I don’t know if there is a better way to get me to the DiscountedSubtotal property I would ideally like.

So to sum up my question, I would like to know the best way to get a DiscountedSubtotal property.

Many thanks and apologies for such a long read 🙂

  • 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-06-13T08:26:48+00:00Added an answer on June 13, 2026 at 8:26 am

    I think the problem here is that you’re not really using a coherent architecture.

    In most cases, you should have a business layer to handle this kind of logic. Then that business layer would have functions like CalculateDiscountForProduct() or CalculateNetPrice() that would go out to the database and retrieve the data you need to complete the business rule.

    The business class would talk to a data layer that returns data objects. Your view only needs it’s view model, which you populate from the business objects returned by your businesss layer.

    A typical method might be:

    public ActionResult Cart() {
        var model = _cartService.GetCurrentCart(userid);
        return View(model);
    }
    

    So when you apply a discount or coupon, you would call a method like _cartService.ApplyDiscount(model.DiscountCode); then return the new model back to the view.

    You might do well to study the Mvc Music Store sample project, as it includes cart functionality and promo codes.

    http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-1

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

Sidebar

Related Questions

I am using the Entity Framework just to create classes that can be mapped
I have this classes who is mapped using Entity Framework Code First: public class
I have a small SQL CE 4.0 database with several tables, mapped using Entity
I have a stored procedure that I've mapped in my entity framework model (trying
I have a table mapped in Entity Framework which works great adding/updating and deleting
I am using Entity Framework and I have two tables, a contact relationship table
In this scenario, I have an existing database with three models: Address , which
I have a field of type image in my database which is mapped as
I'm using Entity Framework 4.2 in code-only mode. I'm letting it autogenerate my database
Is their a way using Entity Framework Code-Only to have an entity that has

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.