Entity Framework with MySQL learning Note One (Common small summary)

Source: Internet
Author: User

1-1 settings

//dataannotation 1-1 | 1-0 table//Sqltable:member, Columns:memberid, name//SQL Basic Logic:1 table PK is another table PK and FK, which is the relationship between 1-1 and 1-0[Table ("member")]     Public classMember {[Key] PublicInt32 MemberID {Get;Set; }  Public stringName {Get;Set; } [Required]/*1-0, here do not required 1-1 relationship is best to put required, in the insert time will not be wrong, but ...                If you write required, then you do update when this address can not be null, that is, must inner join out first, not very convenient. */         Public VirtualAddress Address {Get;Set; } }    //sqltable:address, Columns:memberid, postcode, country[Table ("Address")]     Public classAddress {/*The key is this relationship connection, "member" is TableName*/[Key, ForeignKey ("member")]          PublicInt32 Addressid {Get;Set; }//This is the name Addressid.         Public stringPostcode {Get;Set; }  Public stringCountry {Get;Set; }  Public VirtualMember Member {Get;Set; } }

The basic CRUD

using(Efdb db =NewEfdb ()) {    /*Insert*/Db.members.Add (NewMember {Name="keatkeat", Address=NewAddress {Postcode="81300", Country="Malaysia"        }    }); Db.    SaveChanges (); /*Select*/    varMembers = Db.members.Include (M = m.address). ToList ();//you want the include to come out together, huh ?             /*Update*/    varmember = Db.members.Include (M = m.address). First (). Name ="xinyao123"; Db.    SaveChanges (); /*Delete*/Db.members.Remove (Db.members.Include (M= m.address). First ());//include to delete 2 table data at a time! db. SaveChanges ();}
View Code

1-n settings

    //1-n    /*Basically, if there is no need to write required, it is more convenient to update a single table. (Insert the time you want to control!) )    */[Table ("prod")]     Public classProd {[Key] PublicInt32 ProdId {Get;Set; }  Public stringCode {Get;Set; }  Public Virtualicollection<color> Colors {Get;Set; }//There are multiple colors, with ICollection | List can also} [Table ("Color")]     Public classColor {[Key] PublicInt32 Colorid {Get;Set; } [ForeignKey ("prod")]//Specify an FK         PublicInt32 ProdId {Get;Set; }  Public stringColor {Get;Set; }  Public VirtualProd Prod {Get;Set; }//There is a prod}

Entity Framework with MySQL learning Note One (Common small summary)

Related Article

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.