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