Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6759825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:59:46+00:00 2026-05-26T13:59:46+00:00

I need to know what am I doing wrong, because the generated query doesn’t

  • 0

I need to know what am I doing wrong, because the generated query doesn’t match with the attributes of the data base table and I think that my class was well type and also the mappings. Here’s my code

public class Usuario
{
    #region Atributos
    private int _intID = 0;
    private Perfil _Perfil_FK = null;
    private String _strNombre = "";
    private String _strPassword = "";
    #endregion

    #region Propiedades

    public int ID
    {
        get { return _intID; }
        set { _intID = value; }
    }

    public Nullable<int> IDPerfil_FK { get; set; }

    public virtual Perfil Perfil_FK
    {
        get { return _Perfil_FK; }
        set { _Perfil_FK = value; }
    }
    public String Nombre
    {
        get { return _strNombre; }
        set { _strNombre = value; }
    }
    public String Password
    {
        get { return _strPassword; }
        set { _strPassword = value; }
    }
    #endregion
}

My test was only this _db.Usuario()

The Generated Sql query

SELECT 
[Extent1].[IDUsuario] AS [IDUsuario], 
[Extent1].[IDPerfil_FK] AS [IDPerfil_FK], 
[Extent1].[Nombre] AS [Nombre], 
[Extent1].[Password] AS [Password], 
[Extent1].[PerfilID] AS [PerfilID] <-- this attribute doesn't exit's
FROM [dbo].[Usuario] AS [Extent1];

Here’s my db context class

public class MasterPageAtentoDB : DbContext
{
    public DbSet<Pagina> Pagina { get; set; }
    public DbSet<Perfil> Perfil { get; set; }
    public DbSet<Permiso> Permiso { get; set; }
    public DbSet<Usuario> Usuario { get; set; }

    protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Usuario>().Property(r => r.ID).HasColumnName("IDUsuario");
        modelBuilder.Entity<Pagina>().Property(r => r.ID).HasColumnName("IDPagina");
        modelBuilder.Entity<Permiso>().Property(r => r.ID).HasColumnName("IDPermiso");
        modelBuilder.Entity<Perfil>().Property(r => r.ID).HasColumnName("IDPerfil");
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        base.OnModelCreating(modelBuilder);
    }
}

My database Table

database Table

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:59:46+00:00Added an answer on May 26, 2026 at 1:59 pm

    Entity Framework doesn’ recognize your property IDPerfil_FK as the foreign key property for the Perfil_FK navigation property because you are not following the naming conventions required for automatic FK property detection. As a result EF assumes that IDPerfil_FK is an ordinary scalar property and that Perfil_FK has no exposed FK property in your model and the column in the database has the standard name Perfil_ID (navigation property name + “_” + primary key property name of target entity class).

    You have three options to fix this:

    • Name the FK property appropriately (navigation property name + primary key property name of target entity class):

      public Nullable<int> Perfil_FKID { get; set; }
      
    • Put a data annotation attribute on the property to indicate that it is a FK property:

      [ForeignKey("Perfil_FK")]
      public Nullable<int> IDPerfil_FK { get; set; }
      
    • Define the FK property in Fluent API:

      modelBuilder.Entity<Usuario>()
          .HasOptional(u => u.Perfil_FK)
          .WithMany() // or with parameter if Perfil class refers back to Usuario
          .HasForeignKey(u => u.IDPerfil_FK);
      

    I would prefer the first option because your mapping of the primary key properties relies on conventions anyway, so it would be consequent to follow the conventions for the foreign key properties too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing a project, for which I need to know all the wikipedia
I'm doing some attribute parsing as a string, so I need to know how
I'm doing some work with registry hives, and I need to know if the
does anyone know of a good way of doing the following, I need to
Need to know this so that i could send DTMF and that is going
I need to know about Epoll On linux System. Could you recommend manual or
I need to know how the performance of different XML tools (parsers, validators, XPath
I need to know how much space occupies all the databases inside an SQL
I need to know how to turn on Code Coverage when running TFS builds
I need to know when the memory will be allocated for a particular program.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.