Visual Studio 2012 C# 手动向数据库添加信息
Visual Studio 2012 C# Manually adding information to the database
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using OrdersApp.Models;
namespace OrdersApp.DAL
{
public class OrderInitializer : System.Data.Entity. DropCreateDatabaseAlways<OrderContext>
{
protected override void Seed(OrderContext context)
{
var orders = new List<Order>
{
new Order { OrderId = 1, OrderNumber = 0001, OrderDate = "03/09/2015", OrderTotal = 120, OrderShippingAmount = 12, OrderStatus = "Not Shipped", CustomerEmail = "lostaunaum@gmail.com", CustomerName = "Marco Andre Lostaunau", CustomerAddress1 = "803 Jessie St.", CustomerAddress2 = "", CustomerState = "TX", CustomerZip = 78704, CustomerCity = "Austin", CustomerCountry = "United States"},
new Order { OrderId = 2, OrderNumber = 0002, OrderDate = "03/09/2015", OrderTotal = 120, OrderShippingAmount = 13, OrderStatus = "Not Shipped", CustomerEmail = "lostaunaum@gmail.com", CustomerName = "Tricia Mc.", CustomerAddress1 = "803 Jessie St.", CustomerAddress2 = "", CustomerState = "TX", CustomerZip = 78704, CustomerCity = "Austin", CustomerCountry = "United States"},
new Order { OrderId = 3, OrderNumber = 0003, OrderDate = "03/09/2015", OrderTotal = 120, OrderShippingAmount = 14, OrderStatus = "Not Shipped", CustomerEmail = "lostaunaum@gmail.com", CustomerName = "Yusef", CustomerAddress1 = "803 Jessie St.", CustomerAddress2 = "", CustomerState = "TX", CustomerZip = 78704, CustomerCity = "Austin", CustomerCountry = "United States"},
new Order { OrderId = 4, OrderNumber = 0003, OrderDate = "03/09/2015", OrderTotal = 120, OrderShippingAmount = 14, OrderStatus = "Not Shipped", CustomerEmail = "lostaunaum@gmail.com", CustomerName = "Paul", CustomerAddress1 = "803 Jessie St.", CustomerAddress2 = "", CustomerState = "TX", CustomerZip = 78704, CustomerCity = "Austin", CustomerCountry = "United States"},
};
//context.Orders.Add(orders[0]);
orders.ForEach(o => context.Orders.Add(o));
context.SaveChanges();
}
}
}
这是我的 OrderInitialize.cs 页面,考虑到我的所有文件都是正确的,并且我的数据库已正确连接到我的 visual studio 项目。为什么我的数据库没有将数据植入其中?这是我在 PM 控制台中 运行 Update-Database 后得到的结果。
PM> Update-Database
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
No pending explicit migrations.
Running Seed method.
我正在尝试先为数据库播种,然后创建方法以允许用户手动输入订单删除订单更新订单等......
任何帮助都会很棒。
嘿伙计,谢谢你的回答!对于所有阅读本文的人,我想通了!当您尝试迁移时,请不要将您的迁移内容放在 WhateverNameInitialize.cs 文件中,即使它看起来应该放在那里。您需要将此代码放入您的 Configuration.cs 中,然后您将看到您的迁移实际上填充了您的 SQL 文件! :D –
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using OrdersApp.Models;
namespace OrdersApp.DAL
{
public class OrderInitializer : System.Data.Entity. DropCreateDatabaseAlways<OrderContext>
{
protected override void Seed(OrderContext context)
{
var orders = new List<Order>
{
new Order { OrderId = 1, OrderNumber = 0001, OrderDate = "03/09/2015", OrderTotal = 120, OrderShippingAmount = 12, OrderStatus = "Not Shipped", CustomerEmail = "lostaunaum@gmail.com", CustomerName = "Marco Andre Lostaunau", CustomerAddress1 = "803 Jessie St.", CustomerAddress2 = "", CustomerState = "TX", CustomerZip = 78704, CustomerCity = "Austin", CustomerCountry = "United States"},
new Order { OrderId = 2, OrderNumber = 0002, OrderDate = "03/09/2015", OrderTotal = 120, OrderShippingAmount = 13, OrderStatus = "Not Shipped", CustomerEmail = "lostaunaum@gmail.com", CustomerName = "Tricia Mc.", CustomerAddress1 = "803 Jessie St.", CustomerAddress2 = "", CustomerState = "TX", CustomerZip = 78704, CustomerCity = "Austin", CustomerCountry = "United States"},
new Order { OrderId = 3, OrderNumber = 0003, OrderDate = "03/09/2015", OrderTotal = 120, OrderShippingAmount = 14, OrderStatus = "Not Shipped", CustomerEmail = "lostaunaum@gmail.com", CustomerName = "Yusef", CustomerAddress1 = "803 Jessie St.", CustomerAddress2 = "", CustomerState = "TX", CustomerZip = 78704, CustomerCity = "Austin", CustomerCountry = "United States"},
new Order { OrderId = 4, OrderNumber = 0003, OrderDate = "03/09/2015", OrderTotal = 120, OrderShippingAmount = 14, OrderStatus = "Not Shipped", CustomerEmail = "lostaunaum@gmail.com", CustomerName = "Paul", CustomerAddress1 = "803 Jessie St.", CustomerAddress2 = "", CustomerState = "TX", CustomerZip = 78704, CustomerCity = "Austin", CustomerCountry = "United States"},
};
//context.Orders.Add(orders[0]);
orders.ForEach(o => context.Orders.Add(o));
context.SaveChanges();
}
}
}
这是我的 OrderInitialize.cs 页面,考虑到我的所有文件都是正确的,并且我的数据库已正确连接到我的 visual studio 项目。为什么我的数据库没有将数据植入其中?这是我在 PM 控制台中 运行 Update-Database 后得到的结果。
PM> Update-Database
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
No pending explicit migrations.
Running Seed method.
我正在尝试先为数据库播种,然后创建方法以允许用户手动输入订单删除订单更新订单等......
任何帮助都会很棒。
嘿伙计,谢谢你的回答!对于所有阅读本文的人,我想通了!当您尝试迁移时,请不要将您的迁移内容放在 WhateverNameInitialize.cs 文件中,即使它看起来应该放在那里。您需要将此代码放入您的 Configuration.cs 中,然后您将看到您的迁移实际上填充了您的 SQL 文件! :D –