【发布时间】:2016-10-03 06:41:52
【问题描述】:
我正在尝试启动一个新的 Web 应用程序项目,并且我想使用 asp.net 身份数据库(包含所有 AspNet 表(AspNetUsers、AspNetRoles 等)的数据库)。
我尝试遵循许多指南,其中包括:
- bitoftech.net/2015/01/21/asp-net-identity-2-with-asp-net-web-api-2-accounts-management/
- johnatten.com/2014/04/20/asp-net-mvc-and-identity-2-0-understanding-the-basics/
- tektutorialshub.com/asp-net-identity-tutorial-basics/%20%22ASP.Net%20Identity%20Tutoria
- benfoster.io/blog/aspnet-identity-stripped-bare-mvc-part-1
但是,当我尝试创建数据库时,我得到了这个error。
我也尝试过在 Visual Studio 中模仿模板项目(ASP.NET Core Web Application(.Net Core)),结果相同或this one
这就是我的项目的样子,它基本上是模板减去控制器、视图和模型。
Startup.cs
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath);
builder.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
//var connectionString = @"Data Source=(localdb)\mssqllocaldb;Initial Catalog=Northwind;Integrated Security=True;Pooling=False";
//services.AddEntityFramework()
// .AddSqlServer()
// .AddDbContext<NorthwindContext>(o =>
// o.UseSqlServer(connString));
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app)
{
app.UseDeveloperExceptionPage();
app.UseStaticFiles();
app.UseIdentity();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
ApplicationUser.cs
public class ApplicationUser : IdentityUser
{
}
ApplicationDbContext.cs
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Customize the ASP.NET Identity model and override the defaults if needed.
// For example, you can rename the ASP.NET Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);
}
}
我只想拥有一个带有 asp.net 身份的空项目,最好在 SQL Server 中而不是在 localdb 中。有没有人有一个简单的指南或知道为什么它对我不起作用?
编辑1
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext() : this("Data Source=ACLAP;Initial Catalog=tmpCore;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False") { }
}
EDIT2 我已经把项目放到了github上。
github.com/KiBlob/test
【问题讨论】:
-
我认为您的代码没有任何问题。您的 Visual Studio 似乎有些奇怪。这是将迁移更新推送到数据库的命令。当您在包管理器控制台中键入时,您应该会看到一个下拉选项。此外,可能值得注意的是它是“某种”capsensative,所以我会尝试 Update-Database
-
当我执行“Add-Migration InitialCreate”或“Update-Database”时,我收到此错误消息“System.ArgumentNullException: Value cannot be null. Parameter name: connectionString” 但我基本上已经复制了部分它从 VS 模板中抱怨。
-
您的 Web Config 中有连接字符串吗?
-
@JasonH 不,但我复制的 VS 模板也没有。即使使用网络配置中的连接字符串,我也会遇到同样的错误。
-
尝试将您的 DBContext 构造函数更改为: public ApplicationDbContext() : this("myConnectionString") { }
标签: asp.net-core asp.net-identity asp.net-core-mvc