[英]Entity Framework Tables Primary Key
This are my tables 这是我的桌子
Message 信息
Person 人
This is my Gridview source 这是我的Gridview源
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Select" Text="Select"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="Delete" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="RecipientID" SortExpression="Person.LastName">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Person.LastName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Subject" HeaderText="Subject"
SortExpression="Subject" />
<asp:BoundField DataField="Message1" HeaderText="Message"
SortExpression="Message1" />
<asp:BoundField DataField="Status" HeaderText="Status"
SortExpression="Status" />
<asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" />
<asp:TemplateField HeaderText="SenderID" SortExpression="Person.LastName">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Person.LastName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
This is the result 这是结果
Can you guys help me to display the Sender's name ? 你们可以帮我显示发件人的名字吗? :) Thank you guys in advance! :)预先谢谢你们! I'm using Entity Datasource 我正在使用实体数据源
UPDATE UPDATE
I'm logging in as User Carlo 我以用户Carlo身份登录
Sender display the user that's logged in not the true sender in the Message Table 发件人在消息表中显示已登录的用户而不是真实发件人
this is my Entity Model 这是我的实体模型
You've already got the navigation properties you need for this, but their names conceal their role. 您已经获得了所需的导航属性,但是它们的名称隐藏了它们的作用。 You use Message.Person
to get the sender's name, likewise you can user Message.Person1
to get the recipient's name. 您可以使用Message.Person
来获取发件人的名称,同样,您可以使用Message.Person1
来获取收件人的名称。
I would rename the properties Person
and Person1
into Sender
and Recipient
, respectively. 我将属性Person
和Person1
分别重命名为Sender
和Recipient
。 Thus, they match the primitive foreign key names SenderID
and RecipientID
. 因此,它们与原始外键名称SenderID
和RecipientID
匹配。 Likewise, you could rename the Person.Messages
properties into MessagesSent
and MessagesReceived
. 同样,您可以将Person.Messages
属性重命名为MessagesSent
和MessagesReceived
。
In the template you can use Sender.LastName
and Recipient.LastName
. 在模板中,可以使用Sender.LastName
和Recipient.LastName
。
使用此查询
select m.MessageID,m.ReceipientID,m.Subject,m.senderID,p.UserName,p.FirseName from Message m inner join Person p on m.SenderID= p.ID
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.