0

I'm pretty new to MVC, it's my first project and I'm trying to learn best practices. I'm using Entity Framework.

Here is the thing:

  • I have two models, one is for overtime work records and other one for accounting records.

  • When I create an overtime work record, I want to create an accounting record too. So lets say I created overtime record with 2 hours of work, I want to create an accounting record with value of "hours of work (2) * employees overtime work salary".

  • I can easily do this in OvertimeController's create method but I'm not sure if I should access other models (Accounting Model) from that controller.

What's the best way to do it?

Here is my models, i'm using code first:

Accounting

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int AccountingId { get; set; }
    public decimal AccountingType { get; set; }
    public decimal AccountingMethod { get; set; }
    public string AccountingNote { get; set; }
    public decimal AccountingBorc { get; set; }
    public decimal AccountingAlacak { get; set; }
    public decimal WorkerId { get; set; }
    public decimal PhaseId { get; set; }
    public virtual Worker Worker { get; set; }
    public virtual Phase Phase { get; set; }

Overhours

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int OverhourId { get; set; }
    public int WorkerId { get; set; }
    public int PhaseId { get; set; }
    public int OverhourAmount { get; set; }
    public DateTime OverhourDate { get; set; }
    public virtual Worker Worker { get; set; }
    public virtual Phase Phase { get; set; }

Thanks

  • How does your entity models looks like ? How does your current code for creating a overtime record looks like ? view and action method – Shyju Jan 30 '16 at 15:42
  • I added my models to main post. – user3781428 Jan 30 '16 at 15:46
  • **I want to create an accounting record with value of "hours of work (2) * employees overtime work salary** you have a lot of 8+ properties in your Accounting table/entity. Which property is supposed to store this string value ? What value are you expecting to store in other non null values ( eX : AccountingMethod etc..) ? Where do you get that values from or is there a default value you want to store for all those columns ? Looks like there are some foreign keys as well. Where will you get values for all those columns ? – Shyju Jan 30 '16 at 15:50
  • You select a employee, phase (for example march 2016) from a form. Basically i have the all needed values. For example i will access overtime salary like this: "Overhour.Worker.OverhourSalary". – user3781428 Jan 30 '16 at 15:56
  • In your overtime create method, create a new object of Accounting entity also and save it. – Shyju Jan 30 '16 at 16:01
  • Yes, that's what i was asking. So i can create new models in there and save them from Overtime controller. I know i technically can but is it the right way? I also need to get OverhourSalary from worker object so should i use "db.Workers.Find(WorkerId).OverhourSalary" in Overtime controller? – user3781428 Jan 30 '16 at 16:21
  • 2
    Generally speaking, look into ["view models"](http://stackoverflow.com/a/11074506/304683). Hth. – EdSF Jan 30 '16 at 16:24
  • @EdSF I know what view models are, i will use them but i want to understand the logic first. I basically want to know where should i update related tables in my application. – user3781428 Jan 30 '16 at 16:34

0 Answers0