Entity Framework 4.1-code first specifies the foreign key name

Source: Internet
Author: User

In Entity Framework 4.1, foreign keys are generated in the following ways:

1-if the navigation attribute is specified, a foreign key is automatically generated. The naming rule is "Table name_primary key name"
2-by default, fields with the same primary key name as the navigation property are automatically marked as foreign keys,
3-use [foreignkey] to mark the attribute of the specified object class as a foreign key,
4-upgrade Mode 2. fields with the same primary key name as the navigation attribute are automatically marked as foreign keys, and then the column names in the database corresponding to the field are specified.

Method 3 and 4 specify the names of columns in the generated database.

View code

     Public   Class  User {  Public   Int Userid { Get ; Set  ;}  Public   String Name { Get ; Set  ;}  Public   String Email { Get ; Set  ;}}  Public   Class  Userextinfo {  Public   Int Userextinfoid { Get ; Set ;}  //  1-generate a foreign key: user_userid          Public   Virtual User user { Get ; Set  ;}  //  2-generate a foreign key: userid          Public   Int Userid { Get ; Set  ;} Public   Virtual User user { Get ; Set  ;}  //  3-generate a foreign key: f_userid          Public   Int F_userid { Get ; Set  ;} [Foreignkey (  "  F_userid  " )]  Public   Virtual User user { Get ; Set  ;}  //  4-generate a foreign key: f_userid [Column ( "  F_userid  "  )]  Public   Int Userid { Get ;Set  ;}  Public   Virtual User user { Get ; Set  ;}} 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.