EF代码优先模型与数据库不同步

问题描述 投票:0回答:1

由于某些原因,我的EF Code First模型与数据库不同步。我收到此错误:

{"Invalid column name 'Type_Id1'."}

该字段实际上称为'Type_Id',所以我不确定从1出现的位置。我有一个称为Type_Id的表列,并且在我的类型实体模型中添加了Type_Id。

为什么我会收到该错误消息,以及为什么我在名称末尾得到1?

更新

我的任务班级:

public class Task
    {
        public Task()
        {
            Language = 1;
            Grades = new HashSet<Grade>();
            Categories = new HashSet<Category>();
            Subjects = new HashSet<Subject>();
            Rooms = new Collection<Room>();
            Tools = new Collection<Tool>();
        }

        [Key]
        public int Id { get; set; }

        public string Description { get; set; }

        public virtual TaskType Type { get; set; }

        public string Rules { get; set; }

        [Required]
        [StringLength(200), MinLength(1)]
        public string Name { get; set; }

        public int PreperationTime { get; set; }

        public int InstructionTime { get; set; }

        public int TaskTime { get; set; }

        public int Type_Id { get; set; }

        public string VideoLink { get; set; }

        [Required]
        public int Language { get; set; }

        public int? MinimumParticipants { get; set; }

        public int? MaximumParticipants { get; set; }

        public int? Rating { get; set; }

        [Required]
        public string CreatedBy { get; set; }

        public virtual ICollection<Grade> Grades { get; set; }

        public virtual ICollection<Category> Categories { get; set; }

        public virtual ICollection<Subject> Subjects { get; set; }

        public virtual ICollection<Room> Rooms { get; set; }

        public virtual ICollection<Tool> Tools { get; set; }
    }

DBContext类:

public ApplicationDbContext() : base("DefaultConnection", false)
        {
        }

        public DbSet<Task> Tasks { get; set; }
        public DbSet<TaskType> TaskTypes { get; set; }


        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
entity-framework ado.net ef-code-first code-first ef-migrations
1个回答
1
投票

您需要在导航属性上添加FK属性。 EF正在创建Type_Id1,因为Type_Id已经存在(尽管按照惯例无法确定它是FK)。

[ForeignKey("Type_Id")] 
public virtual TaskType Type { get; set; }

https://msdn.microsoft.com/en-us/data/jj591583.aspx#Relationships

© www.soinside.com 2019 - 2024. All rights reserved.