title: there was an error running the selected code generator: Unable to retrieve MetaData for xxx.Goal.. being caused by a foreign key to application user
body: <p>I am pretty new to asp.net MVC 5 and entityframework 6
  I keep getting the above error when I try to create a controller with views</p>
  
  <p>Here is my model I am trying to create a view for:</p>
  
  <pre><code>public class Goal
      {
          [Key]
          [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
          public int GoalId { get; set; }
          [StringLength(120, ErrorMessage = "Max length is 120 and Min length is {0}", MinimumLength = 4)]
          public string Title { get; set; }
          public GoalStatus Status { get; set; }
  
          public string OwnerUserId { get; set; }        
          public ApplicationUser OwnerUser { get; set; }
  
          public List&lt;GoalPlan&gt; Plans { get; set; }
      }
  </code></pre>
  
  <p>here is my dbcontext class code</p>
  
  <pre><code>public class ApplicationDbContext : IdentityDbContext&lt;ApplicationUser&gt;
      {
          public ApplicationDbContext()
              : base("DefaultConnection", throwIfV1Schema: false)
          {
  
          }
  
  
          public virtual DbSet&lt;Goal&gt; Goals { get; set; }
          public virtual DbSet&lt;GoalPlan&gt; GoalPlans { get; set; }
          public virtual DbSet&lt;GoalTask&gt; GoalTasks { get; set; }
          public virtual DbSet&lt;LinkableItem&gt; LinkableItems { get; set; }      
  
  
          public static ApplicationDbContext Create()
          {
              return new ApplicationDbContext();
          }
      }
  }
  </code></pre>
  
  <p>when I comment out the OwnerUserId lines it works successfully.</p>
  
  <p>The database is created.</p>
  
  <p>I re-built the project each time I changed the code before creating the controller.</p>
  
  <p>This also fails if I use the [ForeignKey("OwnerUser")] attribute on OwnerUserId.</p>
  
  <p>Can anyone help me with why I am getting the metadata error?
  id like to keep learning c# mvc.</p>
  
  <p><strong>EDIT...</strong></p>
  
  <p>so I changed OwnerUserId  to int and that allowed the scaffolding to work. but the problem is not solved...</p>
  
  <p>AspNetUsers is created with the following SQL by the code first initial code migration....</p>
  
  <pre><code>CREATE TABLE [dbo].[AspNetUsers] (
  [Id]                   NVARCHAR (128) NOT NULL,
  [Email]                NVARCHAR (256) NULL,
  [EmailConfirmed]       BIT            NOT NULL,
  </code></pre>
  
  <p>the line in the goal/index method is
      var items = await db.Goals.Include(x => x.OwnerUser).ToListAsync();</p>
  
  <p>in the view is 
      Html.DisplayFor(modelItem => item.OwnerUser.UserName)</p>
  
  <p>and finally which is the issue, the goal table is created as follows</p>
  
  <pre><code>CREATE TABLE [dbo].[Goals] (
  [GoalId]       INT            IDENTITY (1, 1) NOT NULL,
  [Title]        NVARCHAR (120) NULL,
  [Status]       INT            NOT NULL,
  [OwnerUserId]  INT            NOT NULL,
  [OwnerUser_Id] NVARCHAR (128) NULL,
  CONSTRAINT [PK_dbo.Goals] PRIMARY KEY CLUSTERED ([GoalId] ASC),
  CONSTRAINT [FK_dbo.Goals_dbo.AspNetUsers_OwnerUser_Id] FOREIGN KEY 
  ([OwnerUser_Id]) REFERENCES [dbo].[AspNetUsers] ([Id])
  );
  </code></pre>
  
  <p>the value gets stored in OwnerUserId;</p>
  
  <p>thanks again;</p>