.mdf未在App_Data中创建 [英] The .mdf is not being created in App_Data
问题描述
在Visual Studio Ultimate 2013中,我正在处理一个C#ASP.NET Web Forms项目,同时遵循此 http:// goo。 gl / 1hK73 教程(但不完全相同的项目)。但是,我无法在App_Date中创建.mdf文件。
I am working on a C# ASP.NET Web Forms project in Visual Studio Ultimate 2013 while following this http://goo.gl/1hK73 tutorial (but not doing the exact same project). However, I could not make it to create the .mdf file in App_Date.
我已经创建了我的实体类,DbContext,用一个项目种子我的一个表,添加一个connectionString到Web.config:
I have created my entity classes, DbContext, seeded one of my table with a single item, added a connectionString to Web.config:
<add name="TestingSystem" connectionString="Data Source (LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\studenttestingsystem.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
,并在Global.asax.cs中初始化数据库 Application_Start()
方法:
, and initialized the database in Global.asax.cs Application_Start()
method:
Database.SetInitializer(new TestingSystemDatabaseInitializer());
我已经看了一些类似的问题,但没有为我工作(我已经完成了或只是
I have looked up some similar problems, but nothing worked for me (I had it already done or just did not work).
我已经在解决方案资源管理器中刷新了App_Data文件夹,并使用显示所有文件选项,但没有更改。
I have refreshed the App_Data folder in Solution Explorer and used Show All Files option but nothing changed.
我的TestingSystemDatabaseInitializer:
My TestingSystemDatabaseInitializer:
using System.Collections.Generic;
using System.Data.Entity;
namespace Student_Testing_System.Models
{
public class TestingSystemDatabaseInitializer : DropCreateDatabaseIfModelChanges<TestingSystemContext>
{
protected override void Seed(TestingSystemContext context)
{
var root = new Category()
{
CategoryId = 0,
CategoryName = "root",
Description = "Default category",
ParentId = null,
Parent = null,
};
context.Categories.Add(root);
}
}
}
上下文:
public class TestingSystemContext : DbContext
{
public TestingSystemContext()
: base("TestingSystem")
{
}
public DbSet<Question> Questions { get; set; }
public DbSet<Category> Categories { get; set; }
public DbSet<Template> Templates { get; set; }
public DbSet<Test> Tests { get; set; }
public DbSet<StudyGroup> StudyGroups { get; set; }
}
此外,当在PM中通过Update-Database更新数据库时,我得到错误:
EntityType'IdentityUserLogin'没有定义键。定义此EntityType的键。
EntityType'IdentityUserRole'没有定义键。定义此EntityType的键。
Moreover when updating the database via Update-Database in PM I get errors:
EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType.
EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType.
这两个类都是EntityFramework的一部分,没有[Key]的数据注释,所以我不知道如何解决
Both of the Classes are part of EntityFramework and don't have Data Annotations for [Key] so I'm not sure how to solve this.
IdentityUserLogin:
IdentityUserLogin:
public class IdentityUserLogin
{
public IdentityUserLogin();
public virtual string LoginProvider { get; set; }
public virtual string ProviderKey { get; set; }
public virtual IdentityUser User { get; set; }
public virtual string UserId { get; set; }
}
IdentityUserRole:
IdentityUserRole:
public class IdentityUserRole
{
public IdentityUserRole();
public virtual IdentityRole Role { get; set; }
public virtual string RoleId { get; set; }
public virtual IdentityUser User { get; set; }
public virtual string UserId { get; set; }
}
推荐答案
工具 - >库包管理器 - >包管理器控制台并打开打开的控制台
To update a database open Tools->Library Package Manager->Package Manager Console and type in the opened console
update-database
要重新启动LocalDB服务,请在开始/程序菜单 - >所有程序 - >下打开VS.NET的开发人员命令提示符 Visual Studio-> Visual Studio工具
To restart a LocalDB service open the "Developer Command Prompt for VS.NET" under Start/Programs menu->All Programs->Visual Studio->Visual Studio Tools
运行以下命令:
sqllocaldb.exe stop v11.0
sqllocaldb.exe delete v11.0
之后再次执行update-database。
After that execute "update-database" again.
这篇关于.mdf未在App_Data中创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!