下面是一个简单的 ASP.NET Framework MVC Code First 使用 PostgreSQL 的示例。这个示例将展示如何创建一个简单的应用程序,实现对数据库的读写和修改操作。
1. 创建 ASP.NET MVC 项目
首先,创建一个新的 ASP.NET MVC 项目。
2. 安装必要的 NuGet 包
在项目中安装以下 NuGet 包:
EntityFramework
Npgsql
Npgsql.EntityFramework
你可以通过 NuGet 包管理器控制台运行以下命令来安装这些包:
bash
复制
Install-Package EntityFramework
Install-Package Npgsql
Install-Package Npgsql.EntityFramework
3. 配置数据库连接字符串
在 Web.config
文件中,添加 PostgreSQL 数据库的连接字符串:
xml
复制
<connectionStrings>
<add name="DefaultConnection" connectionString="Server=127.0.0.1;Port=5432;Database=YourDatabaseName;User Id=YourUsername;Password=YourPassword;" providerName="Npgsql" />
</connectionStrings>
运行 HTML
4. 创建模型类
在 Models
文件夹中创建一个简单的模型类 Product
:
csharp
复制
namespace YourNamespace.Models
{
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
}
5. 创建数据库上下文
在 Models
文件夹中创建一个 ApplicationDbContext
类,继承自 DbContext
:
csharp
复制
using System.Data.Entity;
namespace YourNamespace.Models
{
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext() : base("DefaultConnection")
{
}
public DbSet<Product> Products { get; set; }
}
}
6. 启用 Code First Migrations
在 NuGet 包管理器控制台中运行以下命令来启用 Code First Migrations:
bash
复制
Enable-Migrations
Add-Migration InitialCreate
Update-Database
这将创建数据库并应用初始迁移。
7. 创建控制器和视图
在 Controllers
文件夹中创建一个 ProductsController
:
csharp
复制
using System.Web.Mvc;
using YourNamespace.Models;
namespace YourNamespace.Controllers
{
public class ProductsController : Controller
{
private ApplicationDbContext db = new ApplicationDbContext();
// GET: Products
public ActionResult Index()
{
return View(db.Products.ToList());
}
// GET: Products/Create
public ActionResult Create()
{
return View();
}
// POST: Products/Create
[HttpPost]
public ActionResult Create(Product product)
{
if (ModelState.IsValid)
{
db.Products.Add(product);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(product);
}
// GET: Products/Edit/5
public ActionResult Edit(int id)
{
var product = db.Products.Find(id);
if (product == null)
{
return HttpNotFound();
}
return View(product);
}
// POST: Products/Edit/5
[HttpPost]
public ActionResult Edit(Product product)
{
if (ModelState.IsValid)
{
db.Entry(product).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(product);
}
// GET: Products/Delete/5
public ActionResult Delete(int id)
{
var product = db.Products.Find(id);
if (product == null)
{
return HttpNotFound();
}
return View(product);
}
// POST: Products/Delete/5
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
var product = db.Products.Find(id);
db.Products.Remove(product);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}
8. 创建视图
在 Views/Products
文件夹中创建相应的视图文件:
Index.cshtml
Create.cshtml
Edit.cshtml
Delete.cshtml
例如,Index.cshtml
可以如下所示:
html
复制
@model IEnumerable<YourNamespace.Models.Product>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Price)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
@Html.ActionLink("Details", "Details", new { id=item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>
</tr>
}
</table>
运行 HTML
9. 运行应用程序
现在你可以运行应用程序,并通过浏览器访问 /Products
路径来查看、创建、编辑和删除产品。
总结
这个示例展示了如何使用 ASP.NET Framework MVC 和 Code First 方法与 PostgreSQL 数据库进行交互。你可以根据需要扩展这个示例,添加更多的功能和复杂性。