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

  • SEARCH
  • Home
  • 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 5988017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:53:57+00:00 2026-05-22T22:53:57+00:00

I am using Entity Framwwork and Code First and getting really confused. I have

  • 0

I am using Entity Framwwork and Code First and getting really confused. I have this class:

public class Blocks
{
    [Display(Name = "ID"),Required(ErrorMessage = "ID is required")]
    [Key,HiddenInput(DisplayValue=false)]
    public int BlockId  { get;set; }

    [Display(Name = "Blocked By"),Required(ErrorMessage = "Blocked By is required")]
    public int ProfileId  { get;set; }

    [Display(Name = "Blocked"),Required(ErrorMessage = "Blocked is required")]
    public int ProfileBlockedId  { get;set; }

    [Display(Name = "Date Blocked"),Required(ErrorMessage = "Date Blocked is required")]
    public DateTime BlockDateTime  { get;set; }

    [Display(Name = "Block Reason")] public string BlockReason  { get;set; }

    public virtual Profiles Profile { get; set; }
    public virtual Profiles ProfileBlocked { get; set; }
}

The profile class is more or less the same and that adds fine and has the correct SQL, but when I run /Blocks I get this error:

MySql.Data.MySqlClient.MySqlException (0x80004005): Unknown column 'Extent1.Profile_ProfileId' in 'field list'
   at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
   at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int32& insertedId)
   at MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32& affectedRows, Int32& insertedId)
  at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId)
   at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
   at MySql.Data.Entity.EFMySqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
   at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
   at System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior    behavior)

this is because the sql produced is:

SELECT 
  `Extent1`.`BlockId`, 
  `Extent1`.`ProfileId`, 
  `Extent1`.`ProfileBlockedId`, 
  `Extent1`.`BlockDateTime`, 
   `Extent1`.`BlockReason`, 
   `Extent1`.`Profile_ProfileId`, 
  `Extent1`.`ProfileBlocked_ProfileId`
  FROM `Blocks` AS `Extent1`

Notice the Profile_ and ProfileBlocked_. I have them virtual so I have a dropdown of profiles when adding or editing or have the profile name when shown on a list. The strange thing is the other tables. Everything has worked fine except for this one.

Here is the code that creates the wrong SQL and breaks:

//
    // GET: /Blocks/
    public ViewResult Index()
    {
        try {
        return View(context.Blocks.Include(blocks => blocks.Profile).Include(blocks => blocks.ProfileBlocked).ToList());
        }
        catch (Exception ex)
        {
           ModelState.AddModelError("",ex.Message);
           CompileAndSendError(ex);
           return View(context.Blocks.ToList());
        }
    }

I am using:
ASP.net MVC 3
Razor Templates
Entity Framework
MVC Scaffolding [custom T4 ]

  • 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-22T22:53:57+00:00Added an answer on May 22, 2026 at 10:53 pm

    Give EF a little hint what are your Foreign Key properties by putting annotations on the properties:

    ...
    [ForeignKey("Profile")]
    public int ProfileId  { get;set; }
    ...
    [ForeignKey("ProfileBlocked")]
    public int ProfileBlockedId  { get;set; }
    ...
    

    I believe that this is always necessary when you have more than one navigation property referencing to the same target class. The conventions don’t detect in this case which properties could be the foreign keys – and EF creates their own FK column names (the Profile_ and ProfileBlocked_ things). And because the column names in the DB are different you get the exception.

    (I think, the problem has nothing to do with properties being virtual or not.)

    Edit

    You can also put the ForeignKey attribute on the navigation properties and specify what’s the name of the FK properties:

    ...
    [ForeignKey("ProfileId")]
    public virtual Profiles Profile { get; set; }
    ...
    [ForeignKey("ProfileBlockedId")]
    public virtual Profiles ProfileBlocked { get; set; }
    ...
    

    This leads to the same mapping and it’s only a matter of taste what you prefer, as far as I can tell.

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

Sidebar

Related Questions

How to declare a one to one relationship using Entity Framework 4 Code First
Is there a way in Entity Framework 4 (using CTP4 and Code First if
I'm trying to map a fairly standard category model using EF Code First public
I am using Entity Framework with my website. To improve performance, I have started
I have a problem with the following Linq query using Entity Framework: from o
So, I am using the Linq entity framework. I have 2 entities: Content and
Okay, so I'm doing my first foray into using the ADO.NET Entity Framework. My
I am attempting to add Entity Framework, code first, to an MVC application that's
Using Entity Framework with MVC2, I have a series of date textboxes, that I
I am having this kind of mysterious issue here. I am currently using Entity

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.