使用代码首先实体框架与.NET MVC 4我创建了一个新的Web应用程序和填充对象的数据库,如下图所示。
public class GratifyGamingContext : DbContext
{
public DbSet<Game> Games { get; set; }
public DbSet<Genre> Genres { get; set; }
public DbSet<UserProfile> UserRepository { get; set; }
}
我想用我的UserRepository表,而不是内置的UserContext类从AccountModel.cs我的用户帐户访问,因为我不能让与我现有的DbContext的工作。
public class UsersContext : DbContext
{
public UsersContext()
: base("DefaultConnection")
{
}
public DbSet<UserProfile> UserProfiles { get; set; }
}
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
}
从构建一个InitializeSimpleMembershipAttribute.cs对象SimpleMembershipInitializer当我的程序总是崩溃。 我注释掉我觉得应该是无关的代码。
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public sealed class InitializeSimpleMembershipAttribute : ActionFilterAttribute
{
private static SimpleMembershipInitializer _initializer;
private static object _initializerLock = new object();
private static bool _isInitialized;
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
// Ensure ASP.NET Simple Membership is initialized only once per app start
LazyInitializer.EnsureInitialized(ref _initializer, ref _isInitialized, ref _initializerLock);
}
private class SimpleMembershipInitializer
{
public SimpleMembershipInitializer()
{
//Database.SetInitializer<UsersContext>(null);
try
{
using (var context = new GratifyGamingContext())
{
if (!context.Database.Exists())
{
// Create the SimpleMembership database without Entity Framework migration schema
((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
}
}
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
}
catch (Exception ex)
{
throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex);
}
}
}
}
我的ConnectionString如下:
<add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=aspnet-GratifyGaming-20120917185558;AttachDbFilename=|DataDirectory|\Games.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
调用任何的AccountController页面WebSecurity.InitializeDatabaseConnect时,我得到了以下错误:
[SQLEXCEPTION(0x80131904):文件目录查找 “C:\用户\虚幻\文档\ Visual Studio 2010的\项目\ GratifyGaming \ GratifyGaming.WebUI \ App_Data文件\ Games.mdf” 与操作系统错误2失败(未能检索文字此错误的原因:15105)。 无法附加文件 'C:\用户\虚幻\文档\ Visual Studio 2010的\项目\ GratifyGaming \ GratifyGaming.WebUI \ App_Data文件\ Games.mdf' 数据库 'ASPNET-GratifyGaming-20120917185558'。
[TargetInvocationException:异常已被调用的目标抛出。] System.RuntimeTypeHandle.CreateInstance(RuntimeType类型,布尔publicOnly,布尔NOCHECK,布尔逻辑canBeCached,RuntimeMethodHandleInternal&构造函数,布尔逻辑bNeedSecurityCheck)+0
我的应用程序,否则连接到数据库,如果我没有去任何的AccountController驱动页面。 如何配置这个应用程序使用,而不是UsersContext用户会员我UserRepository表?