简体   繁体   中英

Entity Framework Foreign Key DataAnnotations

I'm attempting to set up EF classes for a "code-first" approach, using the techniques outlined at the EF page at Data Developer Center , a related SO answer here , another SO thread here , and this article at CodeProject.com.

Two classes need to have one-to-many interaction using data annotations, specifically foreign keys.

Everything seems to be in order with my classes. I can perform a context.Add() , but when saving the changes through context.SaveChanges() , I get the following error message:

The ForeignKeyAttribute on property 'Machines' on type 'BacklogTracker.Models.EFModels.Customer' is not valid. The foreign key name 'MachID' was not found on the dependent type.

Why am I getting this error?

My EF classes and foreign keys seem to be in order, based on the examples and techniques outlined in the links at the beginning of this question... but I can't figure out what's wrong. I'm a beginner at this, so it's very possible I'm missing something entirely.

I'm using VS2013 Express, .NET framework 4.5, EF 6.1.2. Here is code for the classes:

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

public class Customer
{
    public Customer()
    {
        Machines = new List<Machine>();
    }

    [Key]
    public int Number { get; set; }
    public string Name { get; set; }

    public string MachID { get; set; }
    [ForeignKey("MachID")]
    public virtual List<Machine> Machines { get; set; }
}

and

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

public class Machine
{
    public Machine()
    {
        Customer = new Customer();
    }

    [Key]
    public string SN { get; set; }
    public string Model { get; set; }
    public int Hours { get; set; }
    public string Location { get; set; }

    public int CustID { get; set; }
    [ForeignKey("CustID")]
    public virtual Customer Customer { get; set; }
}

You should remove:

public string MachID { get; set; }
[ForeignKey("MachID")]

EF can infer the relationship from the machine side, if I understand you correctly

Also, shouldn't you have ID properties ?

It might be because you are annotating List rather than the Machine class itself. I'm not sure if relationships can be defined on a collection like that.

By your codes, I suppose to that the relationship between customer and machine is one-to-many . One customer has many machines, and a machine is owned by one customer.

The machines are grouped into customer by a foreign key CustID . For those machines with the same CustID , they are under the same customer. If customer has a column named MachID , it will mean that a machine has many customers. It will conflict the fact that one customer has many machines.

@Nathan is right about to remove those codes.

If the MachID should exist, then you need to answer a question about what relationship holds between customer and machine. Is it many-to-many , one-to-many or many-to-one ? If the relationship is many-to-many , then your codes are totally wrong.

public class Customer
{
    [Key]
    public int Number { get; set; }

    // One customer has many machines, so the `MachID` SHOULD NOT exist.
    // public string MachID { get; set; }
    // [ForeignKey("MachID")]
    public virtual List<Machine> Machines { get; set; }
}

public class Machine
{

    [Key]
    public string SN { get; set; }
    ...

    public int CustID { get; set; }
    [ForeignKey("CustID")]
    public virtual Customer Customer { 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