I have 2 tables. bags and people. Each bag was obtained from 1 person first. So I have a foreign key on the bags table field ObtainedFromPerson that references the PersonNumber of the table of People (I've read that the letters ID have some special meaning
in Entity Framework defaults, but unfortunately, I didn't use ID as part of either identifier).
When I read in all the bags from my database, I'd like each bag to also load the class member for the corresponding person. It seems straightforward, but I can't seem to get it right and I don't know enough about the 'magic' EF is performing to figure it
out.
For what it's worth, I have successfully implemented a 1->Many navigation that works great, so I'm baffled as to why this one won't work. I've included the class members for the 1->many just in case they are interfering with the 1->1 somehow.
Here is my bag class (I excised several columns for brevity):
[Table("bagsmvc")]
public partial class Bagsmvc
{
[Key]
[Column("id", TypeName = "int(11)")]
public int Id { get; set; }
[Column(TypeName = "varchar(255)")]
[Required]
public string Airline { get; set; } = "No Airline";
[Column(TypeName = "int(11)")]
public int? ObtainedFromPerson { get; set; }
public int PersonxID { get; set; } // Only 1 person obtained from
public Peoplemvc Personx { get; set; } // This is the person bag was obtained from
public ICollection<Linksmvccore> Links { get; set; }
}
Here is my class for people
[Table("peoplemvc")]
public partial class Peoplemvc
{
[Key]
[Column(TypeName = "int(11)")]
public int PersonNumber { get; set; }
[Column(TypeName = "varchar(255)")]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Column(TypeName = "varchar(255)")]
[Display(Name = "Name")]
public string LastName { get; set; }
public ICollection<Linksmvccore> Links { get; set; } // Each person may be associated with lots of links
// Each person may be associated with lots of bags, but I don't really care about this because it's a collection
// of bags that sent me this bag *first*. The Links table has all bags sent to me by this person, so this
// line is probably unnecessary.
public ICollection<Bagsmvc> Bags { get; set; }
// public Bagsmvc Bag { get; set; } // Each person may be associated with lots of bags
}
And just in case this is causing the problem (indeed, reading this table is what triggers the error)
[Table("linksmvccore")]
public partial class Linksmvccore
{
[Key]
[Column(TypeName = "int(11)")]
public int LinkNumber { get; set; }
[Column("BagID", TypeName = "int(11)")]
public int BagId { get; set; }
[Column("PersonID", TypeName = "int(11)")]
public int PersonId { get; set; }
public Bagsmvc Bag { get; set; }
public Peoplemvc Person { get; set; }
}
And here's the code that reads the Links table (actually it reads every table because it's generic)
// First let's see if there are any foreign key navigation properties into another table (Links table)
// We do this by seeing if there's an ICollection property that references the "many" table
PropertyInfo property = typeof(T1).GetProperties()
.FirstOrDefault(x => x.PropertyType.Name.Contains("ICollection"));
// If no Navigation Property is found, just read the table. Otherwise read the table AND the related table
if (property == null)
{
results = await TableToRead.ToListAsync() as List<T1>;
} else
{
results = await TableToRead.Include(property.Name).ToListAsync() as List<T1>;
}
Earlier in the code, TableToRead is set to _context.Linksmvccore. I realize that as some point I'll need to issue
results = await TableToRead.Include("Personx").ToListAsync() as List<T1>;
to make sure the navigation property is loaded, but it shouldn't break if I don't issue the call. Instead, I get the following error:
An unhandled exception occurred while processing the request.
<div class="titleerror">MySqlException: Unknown column 'b.PersonxID' in 'field list'</div>
MySqlConnector.Core.ServerSession.ReceiveReplyAsyncAwaited(ValueTask<ArraySegment<byte>> task) in ServerSession.cs
, line 774
<div class="titleerror">MySqlException: Unknown column 'b.PersonxID' in 'field list'</div>
MySql.Data.MySqlClient.MySqlDataReader.ActivateResultSet() in MySqlDataReader.cs
, line 130
You can now see why I used the property name PersonxID ... so there would be no ambiguity as to which similarly named field is causing the problem. I even tried stripping out all the navigation properties from the linksmvccore table as
well as the properties in bagsmvc and peoplemvc, but I still get the error.
Any help appreciated.
hello
in my code i created store procedure in database sp_updatepackage and in the where condition of procedure i used package_name which is the first textbox in aspx package name in this where condition i dont want to update package_name because where condition package_name is used
so my requirement is how to update package_name without it use as where is there any change in procedure or in frontend
please execute them?
here is my code
aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" EnableEventValidation="false" CodeBehind="WebForm61.aspx.cs" Inherits="WebApplication14.WebForm61" %>
<asp:Content ID="Content1" ContentPlaceHolderID="title" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="contentbody" runat="server">
<div >
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<table>
<tr>
<td><span style="margin-left:20px">Package name</span><br />
<span style="margin-left:20px"><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</span>
</td>
<td ><span style="margin-left:100px">Reward</span><br />
<span style="margin-left:100px"><asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</span>
</td>
<td><span style="margin-left:100px">Remarks</span><br />
<span style="margin-left:100px"><asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
</span>
</td>
</tr>
</table>
<br />
<br />
<asp:Button ID="Button2" runat="server" Text="Save" OnClick="Button2_Click" />
<asp:button ID="Button3" runat="server" Text="update" OnClick="Button3_Click" />
<br />
<br />
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="both"
AutoGenerateColumns="False"
DataKeyNames="tbl_id" Height="264px" Width="100%"
BorderColor="#FF0066" >
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="Button1" runat="server" Width="25px" OnClick="Button1_Click">edit</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="package_name" HeaderText="Package name" />
<asp:BoundField DataField="reward" HeaderText="Reward" />
<asp:BoundField DataField="remarks" HeaderText="Remarks" />
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
<SelectedRowStyle BackColor="#FF66FF" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</asp:Content>
css
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace WebApplication14
{
public partial class WebForm61 : System.Web.UI.Page
{
SqlCommand cmd = new SqlCommand();
SqlConnection con = new SqlConnection();
string connection = System.Configuration.ConfigurationManager.AppSettings["con"].ToString();
public void EstablishConnection(string storeprocedure)
{
con.ConnectionString = connection;
cmd.Connection = con;
cmd.Connection.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = storeprocedure;
}
public void CloseConnection()
{
cmd.Connection.Close();
cmd.Connection.Dispose();
con.Close();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
FillGridview();
Button3.Visible = false;
}
}
public void FillGridview()
{
SqlDataAdapter adp = new SqlDataAdapter(" select * from tbl_package", connection);
DataTable DT = new DataTable();
adp.Fill(DT);
GridView1.DataSource = DT;
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
LinkButton btn = (LinkButton)sender;
GridViewRow gvr = (GridViewRow)btn.NamingContainer;
TextBox2.Text = gvr.Cells[0].Text;
TextBox3.Text = gvr.Cells[1].Text;
TextBox4.Text = gvr.Cells[2].Text;
Button2.Visible = false;
Button3.Visible = true;
}
protected void Button2_Click(object sender, EventArgs e)
{
EstablishConnection("sp_insert_package");
cmd.Parameters.Add("@package_name", SqlDbType.VarChar, 255).Value = TextBox2.Text;
cmd.Parameters.Add("@reward", SqlDbType.VarChar, 255).Value = TextBox3.Text;
cmd.Parameters.Add("@remarks", SqlDbType.NVarChar, 255).Value = TextBox4.Text;
cmd.Parameters.Add("@by_whom", SqlDbType.VarChar, 255).Value = "1";
cmd.Parameters.Add("@date_time", SqlDbType.VarChar, 255).Value = System.DateTime.Now.ToString();
cmd.Parameters.Add("@status", SqlDbType.VarChar, 255).Value = "1";
ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "anything", "alert('Record Add Sucessfully');", true);
try { cmd.ExecuteNonQuery(); }
catch (Exception ex1) { Response.Write("<script language=javascript>alert('" + ex1.Message.ToString() + ".')</script>"); }
FillGridview();
con.Close();
CloseConnection();
}
protected void Button3_Click(object sender, EventArgs e)
{
EstablishConnection("sp_update_package");
cmd.Parameters.Add("@package_name", SqlDbType.VarChar, 255).Value = TextBox2.Text;
cmd.Parameters.Add("@reward", SqlDbType.VarChar, 255).Value = TextBox3.Text;
cmd.Parameters.Add("@remarks", SqlDbType.NVarChar, 255).Value = TextBox4.Text;
cmd.Parameters.Add("@by_whom", SqlDbType.VarChar, 255).Value = "1";
cmd.Parameters.Add("@date_time", SqlDbType.VarChar, 255).Value = System.DateTime.Now.ToString();
cmd.Parameters.Add("@status", SqlDbType.VarChar, 255).Value = "1";
ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "anything", "alert('Record Add Sucessfully');", true);
try { cmd.ExecuteNonQuery(); }
catch (Exception ex1) { Response.Write("<script language=javascript>alert('" + ex1.Message.ToString() + ".')</script>"); }
FillGridview();
con.Close();
CloseConnection();
}
}
</div> </div>}