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)