我正在尝试在Appointement.dbml中添加多个表。
我拖放Appointement表和医生。
Table Appointement定义为aptId,patientId,doctorId,aptDate,HrDate,IsAvailable。 Table Doctor定义为Id,doctorName。
我需要两个表都能在Listview中显示医生姓名来选择和任命。
我遇到的问题是在Appointement.designer.cs中我遇到了很多错误。
例如:
[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'
我搜索_Doctor_name,结果如下:
[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();
}
}
}
有人可以解释一下Appointement.designer.cs文件有什么问题。我错过了一步吗? 我不明白错误'Doctor'类型已包含'_Doctor_name'
的定义感谢您的帮助
如果它在这里我们做了一些更改来联合两个表而不是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>
以下是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();
};
}
答案 0 :(得分:0)
让DBML中的Doctor表与数据库中的doctor表完全相同,并且AppointmentTable也与数据库中的表完全相同。
现在在您的代码中添加一个类,该类将充当您的UI的视图模型,称之为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
}
现在,在数据层,Repository类中,或者如果需要在.aspx中,编写一个linq查询来构建类,如下所示:
var viewData = from appointment in AppointementDataContext
select new AppointmentDoctorViewModel
{
DoctorId = appointment.Doctor.Id,
DoctorName = appointment.Doctor.Name,
Subject = appointment.Subject
};
使用ObjectDataSource
绑定apx中的ListView。
这为您提供了更多控制,并且可以很好地将数据层与您正在使用的域模型分开。
阅读: