0

I'm having an issue here with Entity Framework 5

In my program I have a base class defined as below:

public abstract class CoreProduct
{
    public abstract decimal Price { get; set; }
    public abstract decimal CategoryID { get; set; }
    public abstract decimal Quantity { get; set; }

    public decimal CalculatePrice()
    {
       //Code to calculate the price here
    }
}

I want to have specific classes auto generated by Entity Framework to inherit from this class, so that I can calculate their prices given the quantity, etc.

So what I did was to create a new file for the normal products and did this:

public partial class Product : CoreProduct
{
    public override decimal Price { get; set; }
    public override decimal CategoryID { get; set; }
    public override decimal Quantity { get; set; }
}

But since the fields Price, CategoryID and Quantity are auto generated by Entity Framework, I have to go delete them from the file auto generated by EF, then everything works.

My problem is that every time I have to update my model at all, the code gets auto generated yet again and I have to go manually re-delete all the fields for all of the classes the inherit my CoreProduct class.

What would be the better way of achieving this so I don't have to manually delete 50 fields every time I have to update my model?

Darkalfx
  • 259
  • 3
  • 12
  • Do you really need those properties to be abstract in the base class? And do you actually do anything in your overrides? – ESG Nov 03 '13 at 14:00
  • I don't actually do much with the properties except override them, my issue is that I couldn't access them from the base class if I only declare them on the parent, unless there's a way I'm unaware of? – Darkalfx Nov 03 '13 at 14:04
  • If they are public (or protected and above, but EF wants public) in the parent, you can access them in the sub classes. Note that when I say parent, I mean having them declared in your CoreProduct class. – ESG Nov 03 '13 at 14:08
  • Even if I did only declare them public on the CoreProduct class, it wouldn't solve my problem of having to manually delete the 50 fields from EF auto generation every time I touch my model, right? How can I change my classes so I wouldn't have to? – Darkalfx Nov 03 '13 at 14:12
  • IF you declare them in the CoreProduct and remove the property declarations from Product, you should not have to delete anything. They should not be generated in the first place. – ESG Nov 03 '13 at 14:15
  • That's my issue though, the properties Price, Quantity and CategoryID are actually columns in my SQL table, thus EF will auto generate them on the Product class no matter what. – Darkalfx Nov 03 '13 at 14:35
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/40461/discussion-between-darkalfx-and-thevedge) – Darkalfx Nov 03 '13 at 14:35

1 Answers1

3

You can create an interface to add to all of your sub classes and create an extension method to do the actual calculations.

For example, I have this interface:

public interface ICoreProduct
{
    decimal Price { get; set; }
    decimal Quantity { get; set; }
}

I created a second partial class for product to attach it to that interface

public partial class Product : ICoreProduct
{
}

And then create an extension method for ICoreProduct

public static decimal CalculatePrice(this ICoreProduct product)
{
    return product.Price * product.Quantity;
}

You should then be able to do something along those lines:

Product prod = new Product();
prod.Price = 2;
prod.Quantity = 10;
decimal price = prod.CalculatePrice();
ESG
  • 8,988
  • 3
  • 35
  • 52