简体   繁体   中英

Entity Framework foreign key not found during migration

I'm experiencing an unexpected error when setting up a migration after adding keys and foreign keys to my data model. I'm using VS2013 Express, with .NET framework 4.5.

When creating a data model for Entity Framework, because the relationship keys between classes aren't what is expected by convention, I'm using data annotations as outlined in the MS Data Developer Center . Here's the class code:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace BacklogTracker.Models
{
    public class WorkOrder
    {
        [Key]
        public string woNum { get; set; }
        public string woClosingStatus { get; set; }
        [ForeignKey("ID")]
        public virtual ICollection<Note> woNotes { get; set; }
        [ForeignKey("machSN")]
        public virtual Machine woMachine { get; set; }
        [ForeignKey("ID")]
        public virtual ICollection<Segment> woSegments { get; set; }
    }

    public class Machine
    {
        [Key]
        public string machSN { get; set; }
        public string machLocation { get; set; }
        public string machModel { get; set; }
    }
    public class Segment
    {
        [Key]
        public int ID { get; set; }
        public uint segNum { get; set; }
        public string segRepair { get; set; }
        [ForeignKey("ID")]
        public virtual ICollection<Note> segNotes { get; set; }
    }
    public class Note
    {
        [Key]
        public int ID { get; set; }
        public DateTime notetimestamp { get; set; }
        public string notestring { get; set; }
    }

}

However, when I try to perform a migration after updating the model by performing enable-migrations in the package manager console, I get the following error:

The ForeignKeyAttribute on property 'woMachine' on type 'BacklogTracker.Models.WorkOrder' is not valid. The foreign key name 'machSN' was not found on the dependent type 'BacklogTracker.Models.WorkOrder'. The Name value should be a comma separated list of foreign key property names.

Why is my foreign key name 'machSN' not being found?

I think you have some errors in your model. Default Code First convention for ForeignKey relationship expected to have declared a foreign key property in the dependend end ( WorkOrder ) that match with primary key property of the principal end ( Machine ). It is not necessary that they have the same name, check this link . So, declare a property named machSN in your WorkOrder class:

public class WorkOrder
{
    [Key]
    public string woNum { get; set; }
    public string woClosingStatus { get; set; }

    public virtual ICollection<Note> woNotes { get; set; }

    public string machSN { get; set; }
    [ForeignKey("machSN")]
    public virtual Machine woMachine { get; set; }

    public virtual ICollection<Segment> woSegments { get; set; }
}

You can find other errors in the woNotes and woSegments navigation properties. In this side of a one-to-many relationship you don't declare a FK, is in the other side, in Note and Segment classes, for example:

public class Note
{
    [Key]
    public int ID { get; set; }
    public DateTime notetimestamp { get; set; }
    public string notestring { get; set; }

    [ForeignKey("Order)]
    public string woNum { get; set; }
    public virtual WorkOrder Order{get;set;}
}

Delete also in the Segment class the ForeignKey attribute over segNotes navigation property for the same reasons explained before.

public class Segment
{
    [Key]
    public int ID { get; set; }
    public uint segNum { get; set; }
    public string segRepair { get; set; }
    public virtual ICollection<Note> segNotes { get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM