EF foreign key renaming, breaking the original constraint rules

Source: Internet
Author: User

I suggest playing code Frist people use System.ComponentModel.DataAnnotations and System.ComponentModel.DataAnnotations.Schema to write entity classes.

Now get to the point, how to break the original foreign key naming rules?

[Table ("Sys_userinfo")] public partial class UserInfo {public UserInfo () {this.            Delflag = false; This.                    R_userinfo_role = new R_userinfo_role ();        } [key]//primary key [Column ("ID")] public int ID {get; set;}        [Column ("UName"), Stringlength ()] public string UName {get; set;}        [Column ("Pwd"), Stringlength ()] public string Pwd {get; set;}        [Column ("Phone"), Stringlength (+)] public string Phone {get; set;}        [Column ("Mail"), Stringlength (+)] public string Mail {get; set;}        [Column ("Subtime")] public System.DateTime subtime {get; set;}        [Column ("Lastmodifiedon")] public System.DateTime Lastmodifiedon {get; set;}                [Column ("Delflag")] public bool Delflag {get; set;}        The [Column ("R_roleid")]//, although defined below, can have the same name as public int RoleID111 {get; set;} [ForeignKey ("RoleID111")]//specifies that the above is a foreign key attribute        Public virtual role Role {get; set;} }

As I commented, when writing a foreign key, you can use the ForeignKey attribute to specify a foreign key attribute, and then rename the column name with column by the specified attribute. Do not need to write the mapping, and look at the time will be more intuitive, but also easy to maintain.

[Table ("Sys_role")]    Public partial class role    {public        role ()        {this            . Delflag = false;            This. UserInfo = new hashset<userinfo> ();        }        [key]//primary Key        [Column ("ID")] public        int ID {get; set;}        [Column ("RoleName"), Stringlength ()]         public string RoleName {get; set;}        [Column ("Delflag")]         public bool Delflag {get; set;}        [Column ("Subtime")]         Public System.DateTime subtime {get; set;}        [Column ("Userinfoid")]        Public virtual icollection<userinfo> UserInfo {get; set;}    }

Then a build in the database

EF foreign key renaming, breaking the original constraint rules

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.