CodeFirst Kevin

1.第一步,创建实体类。

namespace MyMusicStore.Entities
{
    /// <summary>
    /// Song
    /// </summary>
    public class Song
    {
        public int id { get; set; }
        public string name { get; set; }
        public string address { get; set; }
        public string singer { get; set; }
        public string age { get; set; }
        public string type { get; set; }
        public string datatype { get; set; }
        public string length { get; set; }
    }
}

  2.创建Context上下文:

namespace MyMusicStore.Context
{
    /// <summary>
    /// Context Class
    /// </summary>
   public class MusicContext:DbContext
    {
       public DbSet<Song> songs { get; set; }
    }
}

  3.设置数据库连接字符串

<connectionStrings>
    <add name="MusicContext" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=MyMusicStore;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-MyMusicStore.UI-20130227200923.mdf" />
  </connectionStrings>

  4.数据库数据初始化

原文地址:https://www.cnblogs.com/kfx2007/p/2935905.html