简体   繁体   中英

Entity Framework Tables Primary Key

This are my tables

Message 在此处输入图片说明

Person

在此处输入图片说明

This is my Gridview source

<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

I'm logging in as User 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.

I would rename the properties Person and Person1 into Sender and Recipient , respectively. Thus, they match the primitive foreign key names SenderID and RecipientID . Likewise, you could rename the Person.Messages properties into MessagesSent and MessagesReceived .

In the template you can use Sender.LastName and 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM