V
V
Vladislav Sofienko2016-10-31 22:04:19
C++ / C#
Vladislav Sofienko, 2016-10-31 22:04:19

How to fix error in EntityModel?

Hello. After executing this query, namely after join

var patients = from TD in ConnectControl.MHE.Doctors
    join TPatientCards in ConnectControl.MHE.PatientCard on TD.idDoctor equals TPatientCards.idDoctor
    select new
    {
        pc = TPatientCards.idPatientCard
    };

gives an error message
An error occurred while executing the command definition

What is most interesting, such a problem is only with joining with the PatientCard table by its foreign key.
Here is his class
using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;

    public partial class PatientCard
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public PatientCard()
        {
            this.MedAppointments = new HashSet<MedAppointments>();
            this.Patients = new HashSet<Patients>();
            this.Procedure = new HashSet<Procedure>();
        }

        [Key]
        public int idPatientCard { get; set; }
        public int idDoctor { get; set; }
        public int idRoom { get; set; }
        public string diagnosis { get; set; }
    
        public virtual Doctors Doctors { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<MedAppointments> MedAppointments { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<Patients> Patients { get; set; }
        public virtual Rooms Rooms { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<Procedure> Procedure { get; set; }
    }

It seems to me that he is no different from others. What could be such a problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Konstantin Kosyanov, 2016-11-11
@ConstKosyanov

How do you set up relationships between tables? Without any additional configuration, EF expects the <class_name>_Id property.

A
AIgin, 2016-11-15
@AIgin

As Konstantin Kosyanov wrote , if there are no additional link settings (for example, via the Fluent API), then EF follows the foreign key naming convention, which the code does not match. In this case, you need to add the ForeignKey attribute with the name of the corresponding navigation property:

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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question