1

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.

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" />

, 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).

I have refreshed the App_Data folder in Solution Explorer and used Show All Files option but nothing changed.

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

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:
EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType. EntityType 'IdentityUserRole' has no key defined. Define the key for this 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.

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:

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; }
}
Kizivat
  • 183
  • 3
  • 14

1 Answers1

0

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

Run the following commands:

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

After that execute "update-database" again.

user2316116
  • 6,726
  • 1
  • 21
  • 35
  • When I tried the update-database command an error message says that none of my EntityTypes has a key defined. Even-though I have Data Annotation [Key] before my IDs in each of them. o.O – Kizivat May 30 '14 at 10:17
  • Can you give an example of a declaration? – user2316116 May 30 '14 at 10:31
  • `[Key]` `public ulong CategoryId { get; set; }` – Kizivat May 30 '14 at 10:39
  • You cant use unsigned types because there is no e.g. uint or ulong types available in the database. – user2316116 May 30 '14 at 10:49
  • Sure, thanks! However now I still have same kind of errors for IdentityUserLogin and IdentityUserRoles which are not entities I have declared. I am not sure how to solve this in their case. – Kizivat May 30 '14 at 11:22
  • Can you give an example of both declarations? It's hard to answer without seeing the code. – user2316116 May 30 '14 at 11:30
  • Well, they have no Data Annotations, however they are read-only as they are both part of Microsoft's EntityFramework. I will edit the question and copy their code there as I am still not sure how to add code block to comment. – Kizivat May 30 '14 at 11:40