ef代码第一个模型不与db同步

时间:2015-09-10 19:39:04

标签: entity-framework ado.net ef-code-first code-first ef-migrations

出于某种原因,我的EF Code First模型与db不同步。我收到了这个错误:

{"Invalid column name 'Type_Id1'."}

该字段实际上称为“Type_Id”,因此我不确定该字段的出现位置。 我有一个名为Type_Id的表列,并且我在我的类型实体模型中添加了一个Type_Id。

你们有没有想知道为什么我收到错误信息,以及为什么我在名字的末尾得到1?

谢谢,Laziale

更新:

我的任务类:

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

1 个答案:

答案 0 :(得分: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

相关问题