我对 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
我如何尝试使用 asp.net 和 C# 在 gridview 中显示这些数据。
我想PrescribingUnit
用它们显示表的所有数据customers
,Licenses
我正在使用 LinqDataSource,当试图用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>
GridView 数据源是
<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 是
<asp:LinqDataSource ID="LinqDataSource1" runat="server" TableName="licenses"
ContextTypeName="ActivationWebService.LinqToSqlClasses.CustomerLicensesInstallationDataContext"
EntityTypeName="">
</asp:LinqDataSource>
并且CustomerLicensesInstallationDataContext
是自动生成的代码
#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
我的问题是Eval("prescribingunit.unit")
。
我可以显示该表中的数据。如果我将主表从 更改license
为customer
则它适用于表customers
,prescribingunit
但不适用于licenses
.
为了修复它,我在我的数据库(msssql)中创建了一个视图,它现在工作正常。
但我会问你是否有其他解决方案来解决这些情况。