1
public class Person
{
    [Key]
    public int ID { get; private set; }

    [Required]
    [StringLength(100)]
    public string PersonName { get; set; }
}

Creates People table:

ID | PersonName | School_ID | School_ID1

public class School
{
    [Key]
    public int ID { get; private set; }

    [Required]
    [StringLength(100)]
    public string SchoolName { get; set; }

    public virtual List<Person> Students { get; set; }
    public virtual List<Person> Teachers { get; set; }
}

Creates Schools table:

ID | SchoolName

Using the above simple model, Entity Framework (using v6.0) creates two FKs on People table as expected. The mapping is fine - it works, but it doesn't look nice on the database and would prefer to have the FKs called

School_Teacher_ID, School_Student_ID

Any help, very much appreciated.

ADDITIONAL NOTE: I know I can use the ForeignKey DataAnnotation (on School), but this requires me to add the FK (on Person) which is leaking storage concerns into the model and adds overhead that simply isn't necessary.

To expand slightly on the overhead: In the example I was creating to understand this I also created other classes that contained List<Person> (e.g. Club->Members, Company->Employees) and to employ the FK DA I would need to add an additional property for each of the classes using List<Person> on Person.

e.g. Company_Employee_ID, Club_Member_ID, School_Student_ID, School_Teacher_ID

LINK to code: https://www.dropbox.com/s/ataer5zndo0c6bc/Program.cs?dl=0

CONCLUSION SO FAR: Until I get a better understanding of Fluent API my best option looks to be editing the create code produced by enabling Migrations.

Geoff M
  • 21
  • 4
  • exact duplicate of [EF code first - custom foreign key constraint name](http://stackoverflow.com/questions/17926413/ef-code-first-custom-foreign-key-constraint-name) - except for EF version 5/6. – quetzalcoatl Nov 17 '14 at 10:37
  • you may also be interested in this feature: https://entityframework.codeplex.com/workitem/1418 it'd be the best thing for that, however, it's currently in "proposed" state. Maybe they'll add that in future EF versions. – quetzalcoatl Nov 17 '14 at 10:40
  • 1
    @quetzalcoatl The proposed item looks interesting, but I can't make the first response an answer because that post is for Foreign Key Constraint names - I'm just after the FK column name which is looking like I can't do the way I want (keeping a clean model) until I start using migrations – Geoff M Nov 19 '14 at 08:59
  • Oooh sorry, I must have messed this up. I somehow read that you are asking for the constraint names. Sorry once again, close-vote removed!! – quetzalcoatl Nov 19 '14 at 09:25
  • Personally, I think that adding a FK DataAnnotation is not that much different from adding the `Key/Required/Stringlength(100)` annotations, which also adds storage-specific data to your model. – Nzall Nov 19 '14 at 09:26
  • You can use [fluent mapping](http://stackoverflow.com/q/15595818/861716) in the DAL assembly if you don't want to use attributes. (Note that the code in the question is in a `EntityTypeConfiguration` instance). – Gert Arnold Nov 19 '14 at 09:45
  • I completely agree @NateKerkhofs, but with the extra properties required as clarified in question I feel it is a step too far – Geoff M Nov 19 '14 at 10:31
  • @GertArnold I have been trying that route for some time, but all the examples I find have an inverse navigation element and don't use an optional List - they don't match my scenario. – Geoff M Nov 19 '14 at 11:41

0 Answers0