none
Cannot access Data via Foreign Key in MVC5 Visual Studio 2017 RRS feed

  • Question

  • I am pretty new to the .NET Framework. When I am trying to navigate to the Revenue View in the UI, I will get the error ORA-00904:"Extent2"."Currency_Name":Invalid Identifier. It seems that I did something wrong in either my model or the context file, but I couldn't locate the issue. Any suggestion help! Thanks!

    private Context db = new Context();
    

    publicActionResult Revenue(int? id)

               {            if (id ==null)                {                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);                }            var company = db.Companies.Include(c => c.Currency.ExRates)

    .SingleOrDefault(x => x.Id == id);            return View(company);            }                                  

    My Currency model and ExRate model look like this: 

    namespace Project.Models     {     using System.Collections.Generic;     using System.ComponentModel.DataAnnotations;     using System.ComponentModel.DataAnnotations.Schema;     [Table("DATA.CURRENCY")]     publicpartialclassCurrency         {         [System.Diagnostics.CodeAnalysis.SuppressMessage

    ("Microsoft.Usage""CA2214:DoNotCallOverridableMethodsInConstructors")]         public Currency()             {             Companies =newHashSet<Company>();             ExRates =newHashSet<ExRate>();             }         [Key]         [StringLength(26)]         [Column("CURRENCY")]         publicstring Name { getset; }         [System.Diagnostics.CodeAnalysis.SuppressMessage

    ("Microsoft.Usage""CA2227:CollectionPropertiesShouldBeReadOnly")]         publicvirtualICollection<Company> Companies { getset; }         [System.Diagnostics.CodeAnalysis.SuppressMessage

    ("Microsoft.Usage""CA2227:CollectionPropertiesShouldBeReadOnly")]         publicvirtualICollection<ExRate> ExRates { getset; }         }     }

                
    namespace Project.Models
        {
        using System.ComponentModel.DataAnnotations;
        using System.ComponentModel.DataAnnotations.Schema;
     
        [Table("DATA.EX_RATE")]
        public partial class ExRate
            {
            [Key]
            [Column("CURRENCY", Order = 0)]
            [StringLength(26)]
            public string CurrencyName { getset; }
     
            [Column("RATE")]
            public decimal? Rate { getset; }
     
            [Key]
            [Column("YEAR", Order = 1)]
            [StringLength(4)]
            public string Year { getset; }
     
            [Key]
            [Column("QUARTER", Order = 2)]
            [StringLength(2)]
            public string Quarter { getset; }
            }
        }

    namespace Project.Models     {     using System.Data.Entity;     public partial class Context : DbContext         {public Context()

                : base("name=Context")             {             Configuration.LazyLoadingEnabled =false;             }         publicvirtualDbSet<Company> Companies { getset; }         publicvirtualDbSet<Currency> Currencies { getset; }         publicvirtualDbSet<ExRate> ExRates { getset; }         protectedoverridevoid OnModelCreating(DbModelBuilder modelBuilder)

    {             modelBuilder.Entity<Company>()                 .Property(e => e.Region)                 .IsUnicode(false);             modelBuilder.Entity<Company>()                 .Property(e => e.AccountName)                 .IsUnicode(false);             modelBuilder.Entity<Company>()                 .Property(e => e.Id);             modelBuilder.Entity<Company>()                 .Property(e => e.Ticker)                 .IsUnicode(false);             modelBuilder.Entity<Company>()                 .Property(e => e.AccountOwner)                 .IsUnicode(false);             modelBuilder.Entity<Company>()                 .Property(e => e.TeamMember1)                 .IsUnicode(false);             modelBuilder.Entity<Company>()                 .Property(e => e.TeamMember2)                 .IsUnicode(false);             modelBuilder.Entity<Company>()                 .Property(e => e.FiscalYearEndIn)                 .IsUnicode(false);             modelBuilder.Entity<Company>()                 .Property(e => e.LocalCurrencyUnit)                 .IsUnicode(false);             modelBuilder.Entity<Company>()                 .Property(e => e.Group)                 .IsUnicode(false);             modelBuilder.Entity<Currency>()                 .Property(e => e.Name)                 .IsUnicode(false);             modelBuilder.Entity<Currency>()                 .HasMany(e => e.Companies)                 .WithOptional(e => e.Currency)                 .HasForeignKey(e => e.LocalCurrencyUnit);             modelBuilder.Entity<Currency>()                 .HasMany(e => e.ExRates);

    modelBuilder.Entity<ExRate>()                 .Property(e => e.CurrencyName)                 .IsUnicode(false);             modelBuilder.Entity<ExRate>()                 .Property(e => e.Rate)                 .HasPrecision(384);             modelBuilder.Entity<ExRate>()                 .Property(e => e.Year)                 .IsUnicode(false);             modelBuilder.Entity<ExRate>()                 .Property(e => e.Quarter)                 .IsUnicode(false);

    }

    }

    }


    Friday, January 17, 2020 3:13 AM

Answers

  • 1) The code you have posted is C# and not VB.NET with VB.NET being the forum you are posting to.

    2) You are not mentioning that you are using the Oracle database, becuase the ORA-00904 is an Oracle error.

    3) You are not mentioning that you are using the ADO.NET Entity Framework.

    4) You are not mentioning you are using ASP.NET MVC.

    Whether or not your intent is to use C# or VB.NET,  points 2-4  above have forums in ASP.NET forums.

    http://forums.asp.net/

    • Marked as answer by Charlene1227 Monday, January 20, 2020 1:19 AM
    Friday, January 17, 2020 4:24 AM

All replies

  • 1) The code you have posted is C# and not VB.NET with VB.NET being the forum you are posting to.

    2) You are not mentioning that you are using the Oracle database, becuase the ORA-00904 is an Oracle error.

    3) You are not mentioning that you are using the ADO.NET Entity Framework.

    4) You are not mentioning you are using ASP.NET MVC.

    Whether or not your intent is to use C# or VB.NET,  points 2-4  above have forums in ASP.NET forums.

    http://forums.asp.net/

    • Marked as answer by Charlene1227 Monday, January 20, 2020 1:19 AM
    Friday, January 17, 2020 4:24 AM
  • Hi there, 

    Thanks. I will repost in the correct forum. 

    Friday, January 17, 2020 6:04 AM
  • Hello,

    In the future also post code using the code block as with posting images if there is an issue seen no body can copy code and paste into a reply and secondly as shown in the images the code will not compile at all.

    Since this is a C# question in a VB.NET forum this post will be moved to off topic forum as I see you posted the same question to the C# forum then deleted it, if you had never posted in C# I would had moved this to the C# forum.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, January 17, 2020 11:00 AM