每当我尝试在我的网站上打开“用户”标签时出现此错误:“无法将文件”{0}“附加为数据库”{1}“,因此我使用
修复了此问题sqllocaldb.exe stop v11.0
sqllocaldb.exe delete v11.0
在开发人员命令提示符中。
现在我可以在我的网站上打开我的用户标签,但由于我的包含用户的数据库为空,我的迁移功能无效
我试过enable-migrations -contexttypename IssueContext
和update-database
但是会显示图像中的错误:(在新标签中打开以查看)
更新新错误:在新标签页中打开以查看!
Configuration.cs(种子方法)
namespace RecreationalServicesTicketingSystem.Migrations
{
using System;
using System.Collections.Generic;
using System.Data.Entity.Migrations;
using System.Linq;
using RecreationalServicesTicketingSystem.Models;
internal sealed class Configuration : DbMigrationsConfiguration<RecreationalServicesTicketingSystem.DAL.IssueContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
}
protected override void Seed(RecreationalServicesTicketingSystem.DAL.IssueContext context)
{
var users = new List<User>
{
new User { FirstMidName = "Jason", LastName = "Wan",
EnrollmentDate = DateTime.Parse("2016-02-18") },
new User { FirstMidName = "Andy", LastName = "Domagas",
EnrollmentDate = DateTime.Parse("2016-02-18") },
new User { FirstMidName = "Denis", LastName = "Djohar",
EnrollmentDate = DateTime.Parse("2016-02-18") },
new User { FirstMidName = "Christine", LastName = "West",
EnrollmentDate = DateTime.Parse("2016-02-18") },
};
users.ForEach(s => context.Users.AddOrUpdate(p => p.LastName, s));
context.SaveChanges();
var categories = new List<Category>
{
new Category {CategoryID = 0001, Title = "Desktop"},
new Category {CategoryID = 0002, Title = "Mobile"},
new Category {CategoryID = 0003, Title = "Menzits"},
new Category {CategoryID = 0004, Title = "XMPRO"},
new Category {CategoryID = 0005, Title = "Con-X"},
new Category {CategoryID = 0006, Title = "Promapp"},
new Category {CategoryID = 0007, Title = "QGIS"},
};
categories.ForEach(s => context.Categories.AddOrUpdate(p => p.Title, s));
context.SaveChanges();
var tickets = new List<Ticket>
{
new Ticket {
UserID = users.Single(s => s.LastName == "Wan").UserID,
CategoryID = categories.Single(c => c.Title == "Con-X" ).CategoryID,
Issue = ("Test Error 1"),
Priority = Priority.High
},
new Ticket {
UserID = users.Single(s => s.LastName == "Wan").UserID,
CategoryID = categories.Single(c => c.Title == "Desktop" ).CategoryID,
Issue = ("Test Error 2"),
Priority = Priority.Med
},
};
foreach (Ticket e in tickets)
{
var ticketInDataBase = context.Tickets.Where(
s =>
s.User.UserID == e.UserID &&
s.Category.CategoryID == e.CategoryID).SingleOrDefault();
if (ticketInDataBase == null)
{
context.Tickets.Add(e);
}
}
context.SaveChanges();
var departments = new List<Department>
{
new Department { DepartmentID = 1, Name = "IT", Users = new List<User>()},
new Department { DepartmentID = 2, Name = "Admin", Users = new List<User>() },
new Department { DepartmentID = 3, Name = "Human Resources", Users = new List<User>()},
new Department { DepartmentID = 4, Name = "Mechanics" , Users = new List<User>()},
new Department { DepartmentID = 5, Name = "Directors" , Users = new List<User>()},
new Department { DepartmentID = 6, Name = "Operations", Users = new List<User>()}
};
departments.ForEach(s => context.Departments.AddOrUpdate(p => p.Name, s));
context.SaveChanges();
var administrator = new List<Administrator>
{
new Administrator {AdminID = 1, AdminRole = "Administrator LVL1", User = users.Single ( s => s.UserID == 1),
Tickets = new List<Ticket>() },
new Administrator {AdminID = 2, AdminRole = "Administrator LVL2", User = users.Single ( s => s.UserID == 2),
Tickets = new List<Ticket>() },
new Administrator {AdminID = 3, AdminRole = "Administrator LVL3", User = users.Single ( s => s.UserID == 3),
Tickets = new List<Ticket>() }
};
administrator.ForEach(s => context.Administrators.AddOrUpdate(p => p.AdminID, s));
context.SaveChanges();
}
}
}
视图\用户\ Create.cshtml
@model RecreationalServicesTicketingSystem.Models.User
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>User</legend>
<div class="editor-label">
@Html.LabelFor(model => model.LastName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.FirstMidName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.FirstMidName)
@Html.ValidationMessageFor(model => model.FirstMidName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.EnrollmentDate)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.EnrollmentDate)
@Html.ValidationMessageFor(model => model.EnrollmentDate)
</div>
<p> <input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
User.cs
public class User
{
public int UserID { get; set; }
[StringLength(50, MinimumLength = 1)]
public string LastName { get; set; }
[StringLength(50, MinimumLength = 1, ErrorMessage = "First name cannot be longer than 50 characters.")]
[Column("FirstName")]
public string FirstMidName { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime EnrollmentDate { get; set; }
public string FullName
{
get { return LastName + ", " + FirstMidName; }
}
public int AdministratorID { get; set; }
[ForeignKey("AdministratorID")]
public virtual Administrator Administrator { get; set; }
public int DepartmentID { get; set; }
[ForeignKey("DepartmentID")]
public virtual Department Department { get; set; }
public int DepotID { get; set; }
[ForeignKey("DepotID")]
public virtual Depot Depot { get; set; }
public int TicketID { get; set; }
//Setting up relationships A use can apply for any number of tickets, so Tickets is defined as a collection of Ticket entities.
public virtual ICollection<Ticket> Users { get; set; }
}