运行所选代码生成器时发生错误:由于应用程序用户的外键导致无法检索xxx.Goal ..的元数据

时间:2020-06-07 16:52:41

标签: c# asp.net-mvc entity-framework asp.net-mvc-5

对于ASP.NET MVC 5和EntityFramework 6来说,我还是一个新手 当我尝试创建带有视图的控制器时,我不断收到上述错误

这是我要为其创建视图的模型:

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; }
    }

这是我的dbcontext类代码

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();
        }
    }
}

当我注释掉OwnerUserId行时,它可以成功工作。

数据库已创建。

每次在创建控制器之前更改代码都会重新构建项目。

如果我在OwnerUserId上使用[ForeignKey(“ OwnerUser”))属性,这也会失败。

有人能帮助我为什么我遇到元数据错误吗? id喜欢继续学习c#mvc。

编辑...

所以我将OwnerUserId更改为int,这使得脚手架可以工作。但是问题没有解决...

AspNetUsers是使用以下SQL通过代码首次初始代码迁移创建的。...

CREATE TABLE [dbo].[AspNetUsers] (
[Id]                   NVARCHAR (128) NOT NULL,
[Email]                NVARCHAR (256) NULL,
[EmailConfirmed]       BIT            NOT NULL,

目标/指标方法中的行是 var items = await db.Goals.Include(x => x.OwnerUser).ToListAsync();

在视图中是 Html.DisplayFor(modelItem => item.OwnerUser.UserName)

最后是问题所在,目标表创建如下

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])
);

该值存储在OwnerUserId中;

再次感谢;

1 个答案:

答案 0 :(得分:0)

好的,这是我自己的问题的答案

目标属性应为

[ForeignKey("OwnerUser")]
public string OwnerUserId { get; set; }        
public ApplicationUser OwnerUser { get; set; }

与我的原始代码一样,包括注释,但我不确定是否需要...。

然后使用以下流利语言更新了ApplicationDbContext

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);
    }

数据库表已使用字符串正确创建。 控制器脚手架工程 索引方法可以正确显示用户名。