微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在ASP .NET Core Web App中使用Entity Framework ADO.NET?

如何解决如何在ASP .NET Core Web App中使用Entity Framework ADO.NET?

我正在使用Visual Studio 2017中使用ASP .NET Core的Web应用程序,并实现React和Redux。我选择了Visual Studio的React和Redux模板来启动项目。我想使用实体框架的ADO.NET工具创建模型。我已经在WPF桌面应用程序项目中做到了这一点,但从未在Web应用程序中做到过。当我转到Add -> New Item -> Data时,它没有像wpf项目中那样显示ADO.NET选项。是否可以使用此工具来创建模型,还是可以使用网络应用程序? 目前,我正在执行类似的Read和Add方法

[HttpGet("[action]")]
public List < Client > FetchData() {
  List < Client > clients = new List < Client > ();
  var connection = GetConnection();

  var command = new MysqLCommand("SELECT * FROM client;",connection);
  using(MysqLDataReader reader = command.ExecuteReader()) {
    while (reader.Read()) {
      clients.Add(new Client {
        Id = int.Parse(reader.GetValue(0).ToString()),Name = reader.GetValue(1).ToString(),LastName = reader.GetValue(2).ToString()
      });
    }
  }
  return clients;
}

[HttpPost("[action]")]
public Client AddClient() {
  Client client = new Client {
    Name = "Jon",LastName = "Doe"
  };
  MysqLConnection conn = GetConnection();
  using(var command = conn.CreateCommand()) {
    command.CommandText = "INSERT INTO client(NAME,LAST_NAME) VALUES(@name,@lastname)";
    command.Parameters.AddWithValue("@name",client.Name);
    command.Parameters.AddWithValue("@lastname",client.LastName);
    command.ExecuteNonQuery();
    client.Id = (int) command.LastInsertedId;
    conn.Close();
  }
  return client;
}

但是我认为对于很多事情,我可以使用Enitity Framework模型通过linq轻松地完成。 如何创建像ADO.NET这样的模型?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。