Node js and Code First

不想你离开。 提交于 2019-12-09 18:38:29

问题


I've worked on Entity Framework with Code First approach. Now I am learning Node.js I wonder is there a way to make the same code first approach using Node.js and some libraly? I am thinking of using MySql as database.


回答1:


You can look into Sequelize. Example of usage from the home page (I added comments to relate to Code First):

var Sequelize = require('sequelize');
// define your "context"
var sequelize = new Sequelize('database', 'username', 'password');

// define your "entities"
var User = sequelize.define('user', {
  username: Sequelize.STRING,
  birthday: Sequelize.DATE
});

// use them
sequelize.sync().then(function() {
  return User.create({
    username: 'janedoe',
    birthday: new Date(1980, 6, 20)
  });
}).then(function(jane) {
  console.log(jane.get({
    plain: true
  }));
});


来源:https://stackoverflow.com/questions/35010741/node-js-and-code-first

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!