Check out the classes in this file.
It should help you understand how to create the relationships.

Click Here

Hi thanks for your help and the file. I had a good look at it, but to be honest I'm really confused now, and the classes are significantly different from mine, which you know, it's absolutely fine if mine are wrong, but there are things that I don't seem to understand.
Here are the points that are not making that much sense to me, sorry.
1)you said I should use the [key,ForeignKey("Patient")], but in your classes, this is never used anywhere, you only have [Key] in the PatientWeight and Patient classes;
2)I used generic collections in my classes, but you've removed them all. Take the Patient class:
public virtual List<PatientAllergy> PatientAllergies { get; set; } and all the other collections: I used them because one patient can have multiple Allergies, Operations etc, is that wrong? Still in the Patient class, PatientWeight seems to be a collection (you have it down aspublic ICollection<PatientWeight> PatientWeight { get; set; })? Why is that? There's only one weight per patient. Also is a ICollection<> different from a List<>?
3) all the ids on the Patient class:

public int PatientID { get; set; }
public int AllergyID { get; set; }
public int OperationID { get; set; }
public int HealthConditionID { get; set; }
public int PatientBloodTestID { get; set; }

Why storing them in the Patient class and then again in each single class? We essentially have them twice?
4)In each class you have a public virtual ICollection<Patient> Patient { get; set; }, but I thought in your post you said to use public virtual Patient patient {get; set;} instead?
thanks

all right, I've done some work on these darn classes as I need to progress with this.
Based on what tobyITguy and some readings I've done, I have revised the classes, hopefully we're getting closer, even if there are still things that are not clear to me. Here they are:http://pastebin.com/iLdHPQ6X
So, questions now:
1)lets's have a look at some of the classes. Take the Patient.cs:

public virtual ICollection<PatientAllergy>PatientAllergies { get; set; }
    public virtual PatientWeight Weight { get; set; }
    public virtual ICollection<Operation> Operation{ get; set; }
    public virtual ICollection<HealthCondition>ConditionName { get; set; }
    public virtual ICollection<PatientBloodTest> test { get; set; }
    public virtual ICollection<PatientExercises> exercise { get; set; }
    public virtual ICollection<PatientJab> jab { get; set; }

I've used ICollection because the relationship between the patient class and the others is either a 1-to-many or a many-to-many, and the ICollection maintains the many XXX (for example, jabs, operations etc) associated with the patient class. The PatientWeight is different bwcause the relationship is 1-2-many and so, no ICollection. Loosely correct?
2)each other class has a key token with an ID to signify primary key and a foreign key token which is PatientID, for example, take the PatientExercises:

[key]
    public int PatientExercisesID { get; set; }

    [ForeignKey("PatientID")]
    public int PatientID { get; set; }

3)Every class - except Patient of course - also has a reference to the Patient class, depending on the kind of releationship it has with it: if it is 1-2-many or many-2-many, it will have this:
public virtual ICollection<Patient> Patient { get; set; }
if 1-2-1 instead it will have this instead:
public virtual Patient patient { get; set; }
Does it sound correct?
4)for the life of my tobyITguys I can't understand why you decided to declare all these ids inside the Patient class:

public int AllergyID { get; set; }
public int OperationID { get; set; }
public int HealthConditionID { get; set; }
public int PatientBloodTestID { get; set; }

5)the DbContext class: my understanding is that this class is created automatically by the controller, so I haven't created one.
6 - not a question as such:) ICollection<T> vs list<T>: I've read that the former is preferable over the second when it comes down to do Entity Framework stuff
Now, can somebody really kindly have a look at those classes and let me know whether I got it right and if no, why (classes are above in the pastebin link)?
I'm really keen to get this simple application running but ultimately if it is so complicated to do it I can downgrade and create an application with only 2 or 3 classes. If anybody else wants to get involved please feel free to do so, you're more than welcome.
Cheers

Your number 1 is correct.
Your number 2 is correct.
Your number 3 is correct.

For your number 4, you need to add the primary key along with the relationship declartions like you did in 3 or else when your controller is generated you would not see those properties in the view.

That is, after scaffolding, your view would only have name age and height

Your number 5 is correct. Visual Studio can generate that for you.

Your number 6 is correct.

Hope this helps.

thanks for clarifying that, I have amended my patient class now, so that it has all primary keys of all the other clases listed, as below:

public class Patient
{
        [Key]//primary key
        public int PatientID { get; set; }
        public int Height { get; set; }
        public int Age { get; set; }
        public string Name { get; set; }

        public int PatientWeightID { get; set; }
        public int PatientJabID { get; set; }
        public int PatientExercisesID { get; set; }
        public int PatientBloodTestID { get; set; }
        public int OperationID { get; set; }
        public int PatientAllergyID { get; set; }
        public int PatientHealthConditionID { get; set; }


        public virtual ICollection<PatientAllergy>PatientAllergies { get; set; }
        public virtual PatientWeight Weight { get; set; }
        public virtual ICollection<Operation> Operation{ get; set; }
        public virtual ICollection<HealthCondition>ConditionName { get; set; }
        public virtual ICollection<PatientBloodTest> test { get; set; }
        public virtual ICollection<PatientExercises> exercise { get; set; }
        public virtual ICollection<PatientJab> jab { get; set; }
}

so by adding those primary keys back into the patient class, what happens at tables level? All the other tables are linked back to the Patient table? I'll create the controller (I'm planning to create one controller only that will hopefully allow me to enter all the data and test it) and then I'll post back the results, fingers crossed there won't be any problems
thanks

There is a problem with the classes I think http://pastebin.com/Ei6biJss When I created the controller like so:
medical_2_controller.jpg
and I got this error: "Unable to retrieve metadata for MyMedicalRecord_2.Models.Patient." The navigation property 'PatientID' is not a declared property on type 'PatientAllergy'. Verify that is hasn't been explicitly excluded from the model and that it is a valid navigation property"
medical_2_controller_error.jpg
This error doesn't make that much sense to me, especially when the PatientID property is declared in the PatientAllergy class. ANy idea?

Lets see your patient allergy class. The pastebin link isn't working

The link works now. There was a dot attached that shouldn't be there.

Sorry about the dot, copy and paste error, thanks for amending

This is the allergy class tobyITguy:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace MyMedicalRecord_2.Models
{
    public class PatientAllergy
    {
        [Key]
        public int PatientAllergyID { get; set; }

        [ForeignKey("PatientID")]
        public int PatientID { get; set; }

        public string AllergyType { get; set; }

        public virtual ICollection<Patient> Patient { get; set; }
    }
}

The rest is on the pastebin link which works now.

Try this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace MyMedicalRecord_2.Models
{
    public class PatientAllergy
    {
        [Key, ForeignKey("Patient")]
        public int PatientAllergyID { get; set; }
        public string AllergyType { get; set; }
        public virtual ICollection<Patient> Patient { get; set; }
    }
}

Tell me what happens.

OK tobyITguy, I tried the above, but now I get another error when I attempt to create a controller for another class, as it seems it doesn't like that syntax:

[Key, ForeignKey("Patient")]
        public int PatientAllergyID { get; set; }

foreign_key.jpg
"Unable to receive metadata for MyMedicalRecord_2.Models.Patient. The foreign key component PatiendAllergyID is not a declared property on type Patient. Verify it has not been explicitly excluded from the model and that it is a valid primitive property".
Besides, what does this line mean:

[Key, ForeignKey("Patient")]
        public int PatientAllergyID { get; set; }

That PatientAllergyID is primary and foreign key at the same time?!
thanks

tobyITguy, any idea? I mean I'm happy to redo the whole thing from scratch as long as I get clear directions.

Reading from the error, it says PatientAllergyID is not in Patient and should be because you declared it a Foreign Key. Am wondering if it should be called ID, or that the ForeignKey annotation has a parameter that can point to the right property.

Talked to a few people and they're saying that all that notation should be scrapped. Here's an idea, I'll do a simpler example and when I get that to work, I'll come back to this and perhaps re-write it as it seems we're not getting anywhere with this application

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.