时间:2021-07-01 10:21:17 帮助过:14人阅读
2)然后创建MYDBContext.cs
//数据库上下文 public class MYDBContext : DbContext { public MYDBContext() : base("name=conncodefirst") { } public DbSet<UserTs> Customer { get; set; } }View Code
3)在web.config中添加以下代码

<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<connectionStrings>
<add name="conncodefirst" connectionString="server=localhost;port=3306;uid=root;pwd=Ee123;database=MYsqlTs" providerName="MySql.Data.MySqlClient"/>
</connectionStrings>
View Code
然后创建一个控制器
public ActionResult Index() { //CreateTable(); using (MYDBContext db = new MYDBContext()) { try { string SQLStr = string.Format("insert into UserTS VALUES(‘{0}‘, ‘{1}‘)", Guid.NewGuid().ToString(), DateTime.Now.ToString()); db.Database.ExecuteSqlCommand(SQLStr); } catch (Exception ex) { throw; } } return View(); }View Code
运行代码,便可在数据库中看到生成的表

下载地址
【关于mysql8.0安装 】
关于ef+codefirst+mysql(入门)
标签:cut cli cep time comm 下载地址 pen 生成 into