asp.net mvc - Should I follow specific naming pattern for my foreign key, if I want to map my database tables using ado.net Entity Framework? -


I'm reading the following tutorials about the entity framework that uses code-first approach

< P> and mentioned that EF has to use the following 2 naming patterns or rules to interpret the FK between these two classes: -

  public class enrollment {Receive {Public Ent Enrollment ID}; Set; } Public mandatory course ID {Receive; Set; } Receive public enterprise scholarship; Set; } Public grade? Grade {get; Set; } Public Virtual Course Course {get; Set; } Public Virtual Student Student {Received; Set; }} Public class student {public ID} {get; Set; } Public String LastName {get; Set; } Public string FirstMidName {get; Set; } Date of Public Date Date Received; Set; } Public virtual iconging & lt; Nomination & gt; Nomination {Received; Set; }}  

The entity framework then defines a property as a foreign key asset:

  • rule 1 If its name is & lt; Navigation Properties Name & gt; & Lt; Primary key property name & gt; (for example, StudentID for student navigation property) ID).

  • Rule 2 The foreign key properties are also used by the same bus & lt; Primary key property name & gt; can be named, for example, CourseID because the primary key of the Course unit is CourseID ).

But if I already have the database and I am not following this naming pattern, would it be a problem? For example, let's say that I student - & gt; Nomination relation as defined inside of my SQL server Nomination No student --- & gt; Student.studentID , where I define a field called StudentNo as the form of the form for the student.studentid (I did it for testing purposes) , So no trouble for this reason? From my own trial, I have noted that EF nomination Students will be happily mapping with students. Student ID (Even "I do not have a name"), so the above two rules are valid only when using code-first approach. And if I am using DB-first approach then I do not have to follow them?

Thanks

If you are using a database-before, and your If relationships exist in the SQL database, then it should be OK. EF is smart enough to mapping the generated reference code.

If you are using code -first, you do not always have to follow that conference. You can call areas related to your heart's wishes, but to let you know EF What is the relationship between them really? You can do this in one of two ways: With the Fluent API in your DBCentec class, or with data annotation in your unit class (my personal favorite)

To do what you are asking, do the following in your enrollment class:

  Public Entry Enrollment ID {get; Set; } [Foreign students ("students")] do not receive public int student; Set; } Public Virtual Student Student {Received; Set; }  

Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -