Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Navigation Property Woes for 1:1 relationship

$
0
0

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.


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>