Data-driven development mode has been used for a long time: PD designs a database model, generates a database script, and then establishes a database (it may also need to write a SQL script to initialize some data ), then I used an ORM framework and finally began to write crazy.Code.
It's too boring and too troublesome.
The latest EF update seems to start to support the Code-first mode. After reading a few posts, I think this is the Development Mode I want: first write the POCO class and then automatically generate the database (if it is a test, this step may not be done for the time being, and then you can start to write code. The comparison between the simple poco and the congested entities is much more elegant. For other advantages, let alone the models layer in MVC. I really need this.
However, in. net, the code-first mode is not exclusive to EF. Now I will use my favorite subsonic to implement this function. Subsonic is a lightweight ORM solution, and there are many tutorials on the official website. Let's take a look.
First, we need to download a version later than subsonic 3. A version earlier than 3 does not support code-first. In addition, the linqprovider provided by subsonic 3 allows us to apply LINQ for database operations.
First, we design a class: Remember to introduce subsonic
Public class USR {[subsonicprimarykey] public int ID {Get; set;} [subsonicstringlength (50)] Public String username {Get; set;} [subsonicstringlength (50)] [subsonicnullstring] Public String role {Get; set;} public bool systemuser {Get; set;} [subsonicstringlength (50)] Public String password {Get; set ;} public datetime expire {Get; set;} public bool active {Get; Set ;}}
Pay attention to the attribution on the class attribute, which specifies the data type and length used when the database is generated.
With this class, how can we make it generate a database? It's too simple. Look at the code: Remember to introduce subsonic.
Using system; using subsonic. repository; namespace builddbapplication {class program {static void main (string [] ARGs) {console. writeline ("generating database for you... "); _ 001_init build = new _ 001_init (); build. init (); console. writeline ("generated successfully... "); console. writeline ("initialize data... "); // when querying and adding data, you can batch set simplerepositoryoptions. none indicates that the database structure is not changed.
var repo = new simplerepository (" ucerterdb ", simplerepositoryoptions. none); ucenter. entitys. USR model = new ucenter. entitys. USR (); Model. username = "ants"; model. password = "123456"; model. active = true; model. expi Re = datetime. Now; model. systemuser = true; Repo. Add (model); console. writeline ("complete, close the window! "); Console. Read () ;}} internal class _ 0020.init {public void Init () {// you must specify simplerepositoryoptions. runmigrations to change the database structure
VaR repo = new simplerepository ("ucerterdb", simplerepositoryoptions. runmigrations); // generate a table. If the table cannot be queried, the table is automatically generated. Easy! VaR USR = repo. single <ucenter. entitys. USR> (x => X. id = 0); var ticket = repo. single <ucenter. entitys. ticket> (x => X. id = 0 );}}}
Code-first is actually not mysterious. I believe many people like me like this mode. For more information, see the official website.