【发布时间】:2020-01-05 07:27:57
【问题描述】:
我正在使用 MVC dotnet 框架。我有客户模型 如下:
Model Before
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace WebApplication5.Models
{
public class CustomerContext : DbContext
{
public CustomerContext() : base("CustomerDB")
{
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<CustomerContext>());
}
public DbSet<Customer> Customers { get; set; }
}
public class Customer
{
public int id { get; set;}
[Required]
[StringLength(255)]
public String Name { get; set;}
public bool IsSubscribedToNewsletter { get; set;}
public MembershipType MembershipType { get; set;}
public byte MembershipTypeId { get; set; }
}
}
Model After
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Linq;
using System.Web;
using WebApplication5.Configurations;
namespace WebApplication5.Models
{
public class CustomerContext : DbContext
{
public CustomerContext() : base("CustomerContext")
{
Database.SetInitializer<CustomerContext>(new CustomerDBInitializer());
}
public DbSet<Customer> Customers { get; set; }
}
public class Customer
{
public int id { get; set;}
[Required]
[StringLength(255)]
public String Name { get; set;}
public bool IsSubscribedToNewsletter { get; set;}
}
}
下面是我的代码播种CustoemrRepository,它具有删除、更新、编辑和添加客户表中客户的所有功能:
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using WebApplication5.Models;
namespace WebApplication5.Repositories.CustomersRepositories
{
public class CustomerRepository : ICustomerRepository, IDisposable
{
private CustomerContext context;
public CustomerRepository(CustomerContext context)
{
this.context = context;
}
public IEnumerable<Customer> GetCustomers()
{
return context.Customers.ToList();
}
public Customer GetCustomerByID(int Id)
{
return context.Customers.Find(Id);
}
public void InsertCustomer(Customer customer)
{
context.Customers.Add(customer);
}
public void DeleteCustomer(int Id)
{
Customer customer = context.Customers.Find(Id);
context.Customers.Remove(customer);
}
public void UpdateCustomer(Customer customer)
{
context.Entry(customer).State = EntityState.Modified;
}
public void Save()
{
context.SaveChanges();
}
private bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
context.Dispose();
}
}
this.disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
}
这是我的客户控制器:
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Web.Mvc;
using WebApplication5.Models;
using WebApplication5.Repositories.CustomersRepositories;
namespace WebApplication5.Controllers
{
//[RoutePrefix("api/customers")]
public class CustomersController : Controller
{
private ICustomerRepository iCustomerRepository;
public CustomersController()
{
this.iCustomerRepository = new CustomerRepository(new CustomerContext());
}
public CustomersController(CustomerRepository customerRepository)
{
this.iCustomerRepository = customerRepository;
}
[Route("customers")]
[HttpGet]
// GET: Customers
public ActionResult Index()
{
var customers = iCustomerRepository.GetCustomers();
//return View(customers);
return Content("Hey");
}
}
}
之后,我运行了创建本地数据库(Sql Server)和客户表的迁移。 然后我添加了另一个将数据播种到数据库的文件: 我在包管理器中运行了迁移更新命令,但我的客户表仍然没有显示数据并且它是空的。请帮忙 更新种子数据文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebApplication5.Models;
using WebApplication5.Repositories.CustomersRepositories;
namespace WebApplication5
{
public class CustomerDBInitializer : DropCreateDatabaseAlways<CustomerContext>
{
protected override void Seed(CustomerContext context)
{
IList<Customer> customers = new List<Customer>
{
new Customer { id = 1, Name = "Jojo", IsSubscribedToNewsletter = true }
};
var customer1 = new Customer { id = 1, Name = "Jojo", IsSubscribedToNewsletter = true };
context.Customers.AddRange(customers);
base.Seed(context);
}
} }
[my customer table is still empty][1]
[1]: https://i.stack.imgur.com/PJoLm.png
【问题讨论】:
-
在哪里以及如何调用 SeedData() ?
-
检查连接字符串并确保它使用的是您添加数据的同一文件。您可以在计算机上搜索并检查 MDF 文件。
标签: c# sql asp.net .net sql-server