I'm trying to configure relations between tables using Data Annotations of Entity Framework. I've found the following example:
public class Student
{
public Student() { }
public int StudentId { get; set; }
public string StudentName { get; set; }
public int StdandardRefId { get; set; }
[ForeignKey("StandardRefId")]
public virtual Standard Standard { get; set; }
}
public class Standard
{
public Standard()
{
StudentsList = new List<Student>();
}
public int StandardId { get; set; }
public string Description { get; set; }
public virtual ICollection<Student> Students { get; set; }
}
Now, the ForeignKey attribute informs, that StandardRefId is a foregin key. I guess, that EF deduces the target table from type of property (Standard). However, I fail to see, how to define, which column the foreign key refers to. I tried:
[Column("CompanyId")]
public int CompanyId { get; set; }
[ForeignKey("CompanyId")]
[InverseProperty("Id")]
public CompanyDAL Company { get; set; }
However, all I got was the following exception:
An unhandled exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll
Additional information: The property 'Id' cannot be configured as a navigation property. The property must be a valid entity type and the property should have a non-abstract getter and setter. For collection properties the type must implement ICollection where T is a valid entity type.
How can I explicitly say, that CompanyId points to Id property of Company table?
Aucun commentaire:
Enregistrer un commentaire