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
Here is my model I am trying to create a view for:
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<GoalPlan> Plans { get; set; }
}
here is my dbcontext class code
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public virtual DbSet<Goal> Goals { get; set; }
public virtual DbSet<GoalPlan> GoalPlans { get; set; }
public virtual DbSet<GoalTask> GoalTasks { get; set; }
public virtual DbSet<LinkableItem> LinkableItems { get; set; }
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
}
when I comment out the OwnerUserId lines it works successfully.
The database is created.
I re-built the project each time I changed the code before creating the controller.
This also fails if I use the [ForeignKey("OwnerUser")] attribute on OwnerUserId.
Can anyone help me with why I am getting the metadata error? id like to keep learning c# mvc.
EDIT...
so I changed OwnerUserId to int and that allowed the scaffolding to work. but the problem is not solved...
AspNetUsers is created with the following SQL by the code first initial code migration....
CREATE TABLE [dbo].[AspNetUsers] (
[Id] NVARCHAR (128) NOT NULL,
[Email] NVARCHAR (256) NULL,
[EmailConfirmed] BIT NOT NULL,
the line in the goal/index method is var items = await db.Goals.Include(x => x.OwnerUser).ToListAsync();
in the view is Html.DisplayFor(modelItem => item.OwnerUser.UserName)
and finally which is the issue, the goal table is created as follows
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])
);
the value gets stored in OwnerUserId;
thanks again;
ok so here is the answer to my own question
the goal properties should be
[ForeignKey("OwnerUser")]
public string OwnerUserId { get; set; }
public ApplicationUser OwnerUser { get; set; }
as was in my original code, including the annotation, but Im not sure if its requried....
then the ApplicationDbContext was updated with the following fluent
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
modelBuilder.Entity<Goal>()
.HasRequired(x => x.OwnerUser)
.WithMany(x => x.Goals)
.HasForeignKey(x => x.OwnerUserId);
modelBuilder.Entity<GoalTask>()
.HasRequired(x => x.AssignedToUser)
.WithMany(x => x.GoalTasks)
.HasForeignKey(x => x.AssignedToUserId);
}
database table gets created correctly with a string. The controller scaffolding works The index method correctly displays the username.