我对linq实体框架有疑问。所以我有一个名为
的.dbml
文件
CustomerLicensesInstallation.dbml
在这个文件中我有4个mssql db表。
我的桌子和他们的关系如下
表
1. PrescribingUnit
2. Customers
3. Licenses
和
4. Installations
relasoships
Customers
与PrescribingUnit
One To Many
1-N
相关联Customers
Licenses
与One To Many
1-N
Licenses
相关联Installations
One To One
与1-1
PrescribingUnit
customers
相关联Licenses
我如何尝试使用asp.net和C#在gridview中显示这些数据。
我想用Eval
和GridView
来显示表<asp:TemplateField HeaderText="Serial Number" >
<ItemTemplate>
<asp:Label ID="LabelLicenseSerialNumber" runat="server" Text='<%# Eval("licenses.license_serial_number") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Customer Name" >
<ItemTemplate>
<asp:Label ID="LabelCustomerFullName" runat="server" Text='<%# Eval("customers.full_name") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Prescribing Unit" >
<ItemTemplate>
<asp:Label ID="LabelPrescribingUnit" runat="server" Text='<%# Eval("prescribingunit.unit") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
的所有数据
我正在使用LinqDataSource,当尝试使用<asp:GridView ID="GridViewSerials" runat="server" CellPadding="4"
ForeColor="#333333" Width="100%" BorderStyle="Solid" BorderWidth="1px"
CellSpacing="2" ShowHeaderWhenEmpty="True"
ShowFooter="True"
EmptyDataText="No Log Messages" DataSourceID="LinqDataSource1"
AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False"
DataKeyNames="license_id"
PageSize="50"
DataMember="DefaultView">
显示时,它什么也没显示。
我在下面的代码中使用了<asp:LinqDataSource ID="LinqDataSource1" runat="server" TableName="licenses"
ContextTypeName="ActivationWebService.LinqToSqlClasses.CustomerLicensesInstallationDataContext"
EntityTypeName="">
</asp:LinqDataSource>
的列。
CustomerLicensesInstallationDataContext
GridView DataSource是
#pragma warning disable 1591
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.18051
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace ActivationWebService.LinqToSqlClasses
{
public partial class license : INotifyPropertyChanging, INotifyPropertyChanged
{
private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
private long _id;
private System.DateTime _issue_date;
private System.DateTime _expiration_date;
private System.DateTime _installation_date;
private string _serial_number;
private int _number_of_installations;
private bool _is_active;
private long _id_customers;
private long _id_editions;
private EntityRef<installations> _installations;
private EntityRef<customers> _customer;
#region Extensibility Method Definitions
#endregion
public license()
{
this._installations = default(EntityRef<installations>);
this._customer = default(EntityRef<customers>);
OnCreated();
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Name="id", Storage="_id", AutoSync=AutoSync.OnInsert, DbType="BigInt NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
public long license_id
{
get
{
return this._id;
}
set
{
if ((this._id != value))
{
this.Onlicense_idChanging(value);
this.SendPropertyChanging();
this._id = value;
this.SendPropertyChanged("license_id");
this.Onlicense_idChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Name="issue_date", Storage="_issue_date", DbType="Date NOT NULL")]
public System.DateTime license_issue_date
{
get
{
return this._issue_date;
}
set
{
if ((this._issue_date != value))
{
this.Onlicense_issue_dateChanging(value);
this.SendPropertyChanging();
this._issue_date = value;
this.SendPropertyChanged("license_issue_date");
this.Onlicense_issue_dateChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Name="expiration_date", Storage="_expiration_date", DbType="Date NOT NULL")]
public System.DateTime license_expiration_date
{
get
{
return this._expiration_date;
}
set
{
if ((this._expiration_date != value))
{
this.Onlicense_expiration_dateChanging(value);
this.SendPropertyChanging();
this._expiration_date = value;
this.SendPropertyChanged("license_expiration_date");
this.Onlicense_expiration_dateChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Name="installation_date", Storage="_installation_date", DbType="Date NOT NULL")]
public System.DateTime license_installation_date
{
get
{
return this._installation_date;
}
set
{
if ((this._installation_date != value))
{
this.Onlicense_installation_dateChanging(value);
this.SendPropertyChanging();
this._installation_date = value;
this.SendPropertyChanged("license_installation_date");
this.Onlicense_installation_dateChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Name="serial_number", Storage="_serial_number", DbType="NVarChar(256) NOT NULL", CanBeNull=false)]
public string license_serial_number
{
get
{
return this._serial_number;
}
set
{
if ((this._serial_number != value))
{
this.Onlicense_serial_numberChanging(value);
this.SendPropertyChanging();
this._serial_number = value;
this.SendPropertyChanged("license_serial_number");
this.Onlicense_serial_numberChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Name="number_of_installations", Storage="_number_of_installations", DbType="Int NOT NULL")]
public int license_number_of_installations
{
get
{
return this._number_of_installations;
}
set
{
if ((this._number_of_installations != value))
{
this.Onlicense_number_of_installationsChanging(value);
this.SendPropertyChanging();
this._number_of_installations = value;
this.SendPropertyChanged("license_number_of_installations");
this.Onlicense_number_of_installationsChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Name="is_active", Storage="_is_active", DbType="Bit NOT NULL")]
public bool license_is_active
{
get
{
return this._is_active;
}
set
{
if ((this._is_active != value))
{
this.Onlicense_is_activeChanging(value);
this.SendPropertyChanging();
this._is_active = value;
this.SendPropertyChanged("license_is_active");
this.Onlicense_is_activeChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Name="id_customers", Storage="_id_customers", DbType="BigInt NOT NULL")]
public long license_id_customers
{
get
{
return this._id_customers;
}
set
{
if ((this._id_customers != value))
{
this.Onlicense_id_customersChanging(value);
this.SendPropertyChanging();
this._id_customers = value;
this.SendPropertyChanged("license_id_customers");
this.Onlicense_id_customersChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Name="id_editions", Storage="_id_editions", DbType="BigInt NOT NULL")]
public long license_id_editions
{
get
{
return this._id_editions;
}
set
{
if ((this._id_editions != value))
{
this.Onlicense_id_editionsChanging(value);
this.SendPropertyChanging();
this._id_editions = value;
this.SendPropertyChanged("license_id_editions");
this.Onlicense_id_editionsChanged();
}
}
}
[global::System.Data.Linq.Mapping.AssociationAttribute(Name="license_installations", Storage="_installations", ThisKey="license_id", OtherKey="installation_id_licenses", IsUnique=true, IsForeignKey=false)]
public installations installations
{
get
{
return this._installations.Entity;
}
set
{
installations previousValue = this._installations.Entity;
if (((previousValue != value)
|| (this._installations.HasLoadedOrAssignedValue == false)))
{
this.SendPropertyChanging();
if ((previousValue != null))
{
this._installations.Entity = null;
previousValue.license = null;
}
this._installations.Entity = value;
if ((value != null))
{
value.license = this;
}
this.SendPropertyChanged("installations");
}
}
}
[global::System.Data.Linq.Mapping.AssociationAttribute(Name="customers_license", Storage="_customer", ThisKey="license_id_customers", OtherKey="id", IsForeignKey=true)]
public customers customers
{
get
{
return this._customer.Entity;
}
set
{
customers previousValue = this._customer.Entity;
if (((previousValue != value)
|| (this._customer.HasLoadedOrAssignedValue == false)))
{
this.SendPropertyChanging();
if ((previousValue != null))
{
this._customer.Entity = null;
previousValue.licenses.Remove(this);
}
this._customer.Entity = value;
if ((value != null))
{
value.licenses.Add(this);
this._id_customers = value.id;
}
else
{
this._id_customers = default(long);
}
this.SendPropertyChanged("customers");
}
}
}
public event PropertyChangingEventHandler PropertyChanging;
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void SendPropertyChanging()
{
if ((this.PropertyChanging != null))
{
this.PropertyChanging(this, emptyChangingEventArgs);
}
}
protected virtual void SendPropertyChanged(String propertyName)
{
if ((this.PropertyChanged != null))
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
#pragma warning restore 1591
asp:LinqDataSource是
Eval("prescribingunit.unit")
license
是自动生成的代码
customer
我的问题是customers
。
我可以显示此表中的数据。如果我将主表从prescribingunit
更改为licenses
,那么它适用于表{{1}}和{{1}},但不适用于{{1}}。
为了解决这个问题,我在我的数据库(msssql)中创建了一个视图,它现在可以正常工作了。
但是我会问你是否有针对这些情况的其他解决方案。