简体   繁体   English

未在App_Data中创建.mdf

[英]The .mdf is not being created in App_Data

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). 在遵循此http://goo.gl/1hK73教程的同时,我正在Visual Studio Ultimate 2013中从事C#ASP.NET Web窗体项目的工作(但没有做完全相同的项目)。 However, I could not make it to create the .mdf file in App_Date. 但是,我无法在App_Date中创建.mdf文件。

I have created my entity classes, DbContext, seeded one of my table with a single item, added a connectionString to Web.config: 我创建了实体类DbContext,用一个项目将表之一作为种子,并向Web.config添加了一个connectionString:

<add name="TestingSystem" connectionString="Data Source (LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\studenttestingsystem.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />

, and initialized the database in Global.asax.cs Application_Start() method: ,并使用Global.asax.cs Application_Start()方法初始化数据库:

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). 我查找了一些类似的问题,但是对我没有任何帮助(我已经完成了或者只是不成功)。

I have refreshed the App_Data folder in Solution Explorer and used Show All Files option but nothing changed. 我在解决方案资源管理器中刷新了App_Data文件夹,并使用了“显示所有文件”选项,但没有任何更改。

My TestingSystemDatabaseInitializer: 我的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);
        }
    }
}

Context: 语境:

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; }
}

Moreover when updating the database via Update-Database in PM I get errors: 此外,当通过PM中的Update-Database更新数据库时,出现错误:
EntityType 'IdentityUserLogin' has no key defined. EntityType'IdentityUserLogin'没有定义键。 Define the key for this EntityType. 定义此EntityType的键。 EntityType 'IdentityUserRole' has no key defined. EntityType'IdentityUserRole'没有定义键。 Define the key for this EntityType. 定义此EntityType的键。

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. 这两个类都是EntityFramework的一部分,并且没有用于[Key]的数据注释,因此我不确定如何解决这个问题。

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

To restart a LocalDB service open the "Developer Command Prompt for VS.NET" under Start/Programs menu->All Programs->Visual Studio->Visual Studio Tools 要重新启动LocalDB服务,请在“开始/程序”菜单->“所有程序”->“ Visual Studio”->“ Visual Studio工具”下打开“ VS.NET开发人员命令提示符”

Run the following commands: 运行以下命令:

sqllocaldb.exe stop v11.0
sqllocaldb.exe delete v11.0

After that execute "update-database" again. 之后,再次执行“更新数据库”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在App_data中找不到.mdf文件 - Cannot find .mdf file in App_data 使用app_data .mdf数据库进行操作 - Operate with app_data .mdf database 该进程无法访问文件\\ App_Data \\ ASPNETDB.MDF&#39;,因为它正在被另一个进程使用。 - The process cannot access the file \App_Data\ASPNETDB.MDF' because it is being used by another process. 为什么 mdf 文件没有出现在 App_Data 文件夹中? - Why is mdf file not appearing in the App_Data folder? ASP.NET 项目未在 app_data 中创建 .mdf 文件 - ASP.NET project not creating .mdf file in app_data 该进程无法访问文件&#39;C:Project \\ App_Data \\ Database.mdf&#39;,因为它正在被另一个进程使用 - The process cannot access the file 'C:Project\App_Data\Database.mdf' because it is being used by another process GitHub 提交错误:权限被拒绝致命:无法处理路径 ~/App_Data/aspnet-MyProject.mdf - GitHub Commit Error: Permission denied fatal: Unable to process path ~/App_Data/aspnet-MyProject.mdf SQL Server错误:50尝试将.mdf文件添加到App_Data时 - SQL Server error: 50 when trying to add .mdf file to App_Data 在App_Data文件夹中看不到mdf文件,Microsoft Visual Studio Ultimate 2013 - mdf file not visible in App_Data folder, Microsoft Visual Studio Ultimate 2013 ASP.NET MVC中App_Data文件夹下缺少.mdf文件 - .mdf file missing under the App_Data folder in ASP.NET MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM