Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
EF Core 2.1 (using Razor Pages)

I have a table, model and Razor Crud Pages to create new Examination schedules.

The db table and model has a foreign key ID to an Examination Dates table and model, which in turn contains key dates that require adding/updating to an Exam schedule at various stages of the Exam cycle. All these Dates are nullable.

When creating a new Exam schedule, the only field which would (maybe) need to be set is the scheduled date, just this one of 7 dates in the table.

My question is in two parts:

1) How do I create an empty (instantiated but with all nulls) ExaminationDates entry and then attach it's new auto-generated primary key to the Examination Schedule foreign key when the Exam schedule is created? I presume the LinQ sql will be automatic when the models and navigation properties are done correctly, so I don't want to worry about that yet.

2) How do I add a Scheduled date datepicker to the Examination schedule? I only need one, for this scheduled date. All the other dates will be automatically updated at various stages of the course of an examination, which again I think I can do with LinQ easily enough.

I am wondering about having a navigation property on the Examination for this scheduled date. I'm also worrying about Page Models, which i only partially understand. It's easy to do crud with single lookup drop-down values but with a table / model containing a whole load of dates, how would this work? I've been coding C# for many years but I'm new to EF Core and Entity Framework. As is usual in life, I've read hundreds of pages but none seem to replicate what I need to do.

Any help would be appreciated.

What I have tried:

I've tried reading all sorts of different pages.
Posted
Updated 31-Jul-18 5:01am
v2

1 solution

1) This isn't going to be specific to your situation because you're not posting any code so I can't see into your data model. But if you're using EF then you don't have to worry about the primary key foreign key values because that's entity framework's job. Your DBContext will have a data structure for the parent type records that includes a list of the child type records. You create the parent entity within the DBContext, you add in child entities to the the list belonging to that parent entity and you tell the DBContext to SaveChanges()
Naturally there are variations of this process, there's an Async version, one that's cancellable, etc. But the EF will take care of getting the ID from the parent record and including it with the child records (provided your DBContext data is structured correctly).

2) Not enough info about your process to be able to assist here.

HTH, Mike
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900