0

I am trying to put more than 1 table in my Appointement.dbml.

I drag and drop the Appointement table and the doctor.

Table Appointement is define as aptId, patientId, doctorId, aptDate, HrDate, IsAvailable. Table Doctor is define as Id, doctorName.

I need both table to be able to display the doctor name in the Listview to choose and appointement.

The problem I got is in the Appointement.designer.cs I got many errors.

Ex.:

[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Doctor")]
public partial class Doctor : INotifyPropertyChanging, INotifyPropertyChanged
{

private static PropertyChangingEventArgs emptyChangingEventArgs = new 
    PropertyChangingEventArgs(String.Empty);

private int _Id;

private string _Doctor_name;  <=== Error The type 'Doctor' already contains a definition for 
                                        '_Doctor_name'

I search for _Doctor_name, here are the results:

[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Doctor_name", 
    DbType="NVarChar(50) NOT NULL", CanBeNull=false)]
public string Doctor_name
{
    get
    {
        return this._Doctor_name;
    }
    set
    {
        if ((this._Doctor_name != value))
        {
            this.OnDoctor_nameChanging(value);
            this.SendPropertyChanging();
            this._Doctor_name = value;
            this.SendPropertyChanged("Doctor_name");
            this.OnDoctor_nameChanged();
        }
    }
}

Can someone explain me what is wrong with the Appointement.designer.cs file. Did I miss one step ? I don't understand the error The type 'Doctor' already contains a definition for '_Doctor_name'

Thanks for your help

Should it be here we made some changes to joint the two table instead of in the xxx.designer.cs?

 <asp:LinqDataSource ID="LinqDataSource1" runat="server" 
    ContextTypeName="AppointementDataContext" 
    EnableDelete="True" EnableInsert="True" EnableUpdate="True" EntityTypeName="" 
    TableName="Appointement" 
        Where="IsAvailable = True && dateApt >= DateTime.Now && doctorId = @doctorId"> 

            <WhereParameters>
               <asp:ControlParameter 
                   Name="doctorsId" 
                   ControlID="DropDownList1" 
                   PropertyName="SelectedValue"

                   Type="String" />
             </WhereParameters>

        </asp:LinqDataSource>

Here is the code in the AppointementDoctor.aspx.cs

private void ListAppointement(int iIndexDdl)
{
    using (AppointementDataContext db = new AppointementDataContext())
    {
        var rdvItems = from Appointement in db.Appointement
                       where Appointement.doctorId == iIndexDdl && Appointement.isAvailable == 
        true && Appointement.dateApt >= DateTime.Now 
                       select Appointement;


                  ListView1.DataSourceID = null;
                  ListView1.DataSource = rdvItems;

                  int numberOfRecords = rdvItems.Count();
                  if (numberOfRecords == 0)
                  {
                      lblMessage.Text = "No Appointement is available";
                  }

                  ListView1.DataBind();
    };
}
user3127986
  • 388
  • 1
  • 10
  • 33
  • In appointment you should not map fields or properties to the Doctor table. You should use a LINQ query to fetch the additional fields from the related table, your datamodel should resemble the database. Create View Models if needed for complex situations. – jessehouwing Jan 26 '14 at 15:55
  • I should create a View between the two tables and put this view instead of the two tables ? Remember this is to populate a ListView, now this Listview doesn't see the Doctor name since it is just a key to the Doctor table – user3127986 Jan 26 '14 at 15:59
  • sorry I fixed the error it should be this._Doctor_name – user3127986 Jan 26 '14 at 16:00
  • Still, is this generated code? Normally, `Appointement` should have a reference property (or 'navigation property') to `Doctor`, so you can do something like `appointment.Doctor.Name`. – Gert Arnold Jan 26 '14 at 16:04
  • Yes it is generated code in the Appointement.designer.cs and I got many errors in the file. Can you explain me the reference from Appointement you are talking about ? Maybe the problem is I should not put more than 1 table in the Appointement.dbml file – user3127986 Jan 26 '14 at 16:08
  • @GertArnold can you explain me why it complains about the _Doctor_name error does "_Doctor_name" = "Doctor_name" without an underscore? – user3127986 Jan 26 '14 at 16:16
  • @jessehouwing Are you suggestint I should create a view instead of trying to get two tables in the Appointement.dbml file ? I am trying to populate a ListView. I added more code from my AppointementDoctor.aspx thanks for your help – user3127986 Jan 26 '14 at 16:24
  • I don't know. Of course a dbml can contain multiple tables. Normally it should be enough to drag the tables onto the designer surface from the server explorer. – Gert Arnold Jan 26 '14 at 16:24
  • No you don't create a Database View, you create a class AppointmentListView and you create that from a LINQ query. – jessehouwing Jan 26 '14 at 16:27
  • @GertArnold here are the steps I have perform. I deleted the Appointement.designer.cs. I open the Appointement.dbml, I deleted the Appointement table. I drag and drop the Appointement table and also the Doctor table. Save the Appointement.dbml. Generate the code and I got those errors. – user3127986 Jan 26 '14 at 16:27
  • @jessehouwing I added the AppointementDoctor.aspx.cs. Normally I used the AppointementDataContext to populate my ListView. But since I normalized my tables i don't get the doctor_name anymore but just his doctor_Id. Can you explain why I got the error I was mentionning earlier with the _Doctor_name; <=== Error The type 'Doctor' already contains a definition for '_Doctor_name' – user3127986 Jan 26 '14 at 16:39
  • 1
    It finally dawned to me what is wrong here. Maybe I'm terribly wrong, but from your language ("Appointement.dbml") I conclude that you create a *dbml per table*. So when you introduce `Doctor` into your `Appointement.dbml` the generated `Doctor` type conflicts with the existing (but also partial) type `Doctor` from `Doctor.dbml`. Effectively this means that all properties will be marked as duplicated by the compiler. The remedy is to have only *one* dbml containing all tables of your data model. – Gert Arnold Jan 26 '14 at 18:29
  • @GertArnold I agree. I have one doctor.dbml which I was using for a WCF service to populate my dropdownlist choice. When I put the doctor table in the appointement.dbml it generates twice the properties. This is going in the wrong direction for me. I cannot delete the WCF service for the Doctor.dbml. I need it for the WCF. – user3127986 Jan 26 '14 at 18:37
  • Ah! Why can't you use one dbml for both? Anyway, you can always rename the `Doctor` type in the `Appointment.dbml`. – Gert Arnold Jan 26 '14 at 18:44
  • @GertArnold I went to my Doctor.designer.cs and I changed the class to DoctorA and public DoctorA() I got an error in the Appointement.aspx.cs when I am calling the WCF service reference Additional information: An error occurred while receiving the HTTP response to http://localhost:49178/DoctorService.svc. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details. – user3127986 Jan 26 '14 at 19:42
  • here how it is define private List DoctorDetail() { DoctorServiceReference.DoctorServiceClient client = new DoctorServiceReference.DoctorServiceClient(); DoctorServiceReference.Doctor[] doctors = client.DoctorDetail(); return doctors.ToList(); } – user3127986 Jan 26 '14 at 19:44
  • Now you changed the service contract. I think you should rename the other `Doctor` (for appointment). By the way, you should do the renaming in the dbml designer through the properties window. – Gert Arnold Jan 26 '14 at 20:00
  • @GertArnold I took your advice and I rename the Doctor (for Appointement) using the properties window for the .dbml file. Everything is now compiling. need to debug now. Thanks a lot for your help. – user3127986 Jan 27 '14 at 13:37

1 Answers1

0

Have the Doctor table in your DBML be exactly like the doctor table in your database and teh AppointmentTable exactly the same as in your database too.

Now in your code add a class which will act as your View Model for your UI, call it say AppointmentDoctorViewModel..

public class AppointmentDoctorViewModel
{ 
    public int DoctorId {get;set;}
    public string DoctorName {get;set;}
    public string Subject{get;set;}
    // etc, other fields for your ListView
}

Now, in a Data Layer, Repository class or if need be in your .aspx, write a linq query that builds up the class like this:

var viewData = from appointment in AppointementDataContext
    select new AppointmentDoctorViewModel
    {
        DoctorId = appointment.Doctor.Id,
        DoctorName = appointment.Doctor.Name,
        Subject = appointment.Subject
    };

The use an ObjectDataSource to bind against the ListView in your apx.

This gives you a lot more control and nicely separates the data layer from the Domain Model you're working with.

Read up on:

Community
  • 1
  • 1
jessehouwing
  • 106,458
  • 22
  • 256
  • 341