关于使用EF CodeFirst创建数据库无法连接的问题

bakalr 2017-12-15 09:29:14
新人,最近在学MVC,在使用EF CodeFirst模式下创建好了实体,上下文关系后
运行就失败
实体类

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace MVC_Diary.Models
{
public class Diary
{
[DisplayName("Id")]
public int Id { get; set; }

[DisplayName("Title"), Required]
public string Title { get; set; }

[DisplayName("Content"), Required]
public string Content { get; set; }

[DisplayName("PubDate"),DataType(DataType.DateTime)]
public DateTime? PubDate { get; set; }

[DisplayName("UserId")]
public int UserId { get; set; }

[DisplayName("User")]
public virtual User User { get; set; }
}
}

上下文

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace MVC_Diary.Models
{
public class DiaryDB:DbContext
{
public DbSet<User> Users { get; set; }
public DbSet<Diary> Diaries { get; set; }
}
}

初始化

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace MVC_Diary.Models
{
public class SampleData:DropCreateDatabaseAlways<DiaryDB>
{
protected override void Seed(DiaryDB context)
{
context.Users.Add(new User
{
UserName="LiaoRain",
PassWord="LiaoRain",
Diaries = new List<Diary>
{
new Diary{Title="title01",Content="content01",PubDate=System.DateTime.Now},
new Diary{Title="title02",Content="content02",PubDate=System.DateTime.Now},
new Diary{Title="title03",Content="content03",PubDate=System.DateTime.Now},
new Diary{Title="title04",Content="content04",PubDate=System.DateTime.Now}
}
});
context.Users.Add(new User
{
UserName = "Bear",
PassWord = "Bear",
Diaries = new List<Diary>
{
new Diary{Title="title05",Content="content05",PubDate=System.DateTime.Now},
new Diary{Title="title06",Content="content06",PubDate=System.DateTime.Now},
new Diary{Title="title07",Content="content07",PubDate=System.DateTime.Now},
new Diary{Title="title08",Content="content08",PubDate=System.DateTime.Now}
}
});
base.Seed(context);
}
}
}

全局类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Data.Entity;
using MVC_Diary.Models;

namespace MVC_Diary
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
Database.SetInitializer(new SampleData());
}
}
}


config

<?xml version="1.0" encoding="utf-8"?>
<!--
有关如何配置 ASP.NET 应用程序的详细信息,请访问
http://go.microsoft.com/fwlink/?LinkId=301880
-->
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>

<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
<connectionStrings>
<add name="MVC_Diary.Models.DiaryDB" connectionString="Data Source=.; Database=DiaryDB; Uid=sa; Pwd=wlb4-6" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" >

</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>

运行时产生如下问题,但使用此账号可以登录sqlserver
...全文
560 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
正怒月神 版主 2017-12-15
  • 打赏
  • 举报
回复
不向dbcontext注入字符串连接。那是没办法登录数据库的。 除非你在程序里手动指明dbcontext的connection。
正怒月神 版主 2017-12-15
  • 打赏
  • 举报
回复
你没有初始化 连接字符串啊。 public class DiaryDB:DbContext { public DiaryDB() : base("name=MVC_Diary.Models.DiaryDB") { } public DbSet<User> Users { get; set; } public DbSet<Diary> Diaries { get; set; } }
lovebaby 2017-12-15
  • 打赏
  • 举报
回复
数据库连接字符串错误?
bakalr 2017-12-15
  • 打赏
  • 举报
回复
准确的说,我在控制台下,如果使用的是Context.Database.CreateDatabaseIfNotExists,就是可以根据实体类创建DB 但如果使用初始化器中的CreateDatabaseIfNotExists、DropCreateDatabaseIfModelChanges、DropCreateDatabaseAlways均不能够创建DB
bakalr 2017-12-15
  • 打赏
  • 举报
回复
我在SQLSERVER中建好了数据库再执行,并没有看到初始化的这些内容是为什么呢
全栈极简 2017-12-15
  • 打赏
  • 举报
回复
你在sqlserver中创建好DiaryDB数据库后再试。
正怒月神 版主 2017-12-15
  • 打赏
  • 举报
回复
引用 12 楼 lrprocedure 的回复:
就是写好上述那些代码片,执行的时候没有看到数据库生成 现在还没有写CRUD,是不是必须写CRUD才能生成数据库呢,初学MVC,很多不理解的地方请谅解
你写一个crud,然后运行一下就可以了。 当然也有其他解决方案: http://blog.csdn.net/hanjun0612/article/details/68942059 选一个设置项就可以了。
bakalr 2017-12-15
  • 打赏
  • 举报
回复
引用 10 楼 hanjun0612 的回复:
[quote=引用 9 楼 lrprocedure 的回复:] 我写好这些仍然没有建立起数据库,而数据库迁移那种虽然可以建立起,但是却不能初始化数据?还请版主大大解惑
我这边没这个问题。 我记得只要搭建好模型,然后写一小段代码,比如查询之类的,然后运行这个查询方法。就可以到数据库自动创建表了。 你说的:我写好这些仍然没有建立起数据库 是指运行了查询方法或者新增方法后,数据库没建立。 还是压根就没有运行过上面的方法?[/quote] 就是写好上述那些代码片,执行的时候没有看到数据库生成 现在还没有写CRUD,是不是必须写CRUD才能生成数据库呢,初学MVC,很多不理解的地方请谅解
正怒月神 版主 2017-12-15
  • 打赏
  • 举报
回复
其实我比较纳闷的是你的错误。 到底是数据库无法连接, 还是数据库无法更新。
正怒月神 版主 2017-12-15
  • 打赏
  • 举报
回复
引用 9 楼 lrprocedure 的回复:
我写好这些仍然没有建立起数据库,而数据库迁移那种虽然可以建立起,但是却不能初始化数据?还请版主大大解惑
我这边没这个问题。 我记得只要搭建好模型,然后写一小段代码,比如查询之类的,然后运行这个查询方法。就可以到数据库自动创建表了。 你说的:我写好这些仍然没有建立起数据库 是指运行了查询方法或者新增方法后,数据库没建立。 还是压根就没有运行过上面的方法?
bakalr 2017-12-15
  • 打赏
  • 举报
回复
引用 5 楼 hanjun0612 的回复:
你没有初始化 连接字符串啊。 public class DiaryDB:DbContext { public DiaryDB() : base("name=MVC_Diary.Models.DiaryDB") { } public DbSet<User> Users { get; set; } public DbSet<Diary> Diaries { get; set; } }
我写好这些仍然没有建立起数据库,而数据库迁移那种虽然可以建立起,但是却不能初始化数据?还请版主大大解惑
正怒月神 版主 2017-12-15
  • 打赏
  • 举报
回复
引用 7 楼 lrprocedure 的回复:
现在是通过这种方式解决的,版主大大说的那个连接字符串之前也使用过,也没创建起 解决办法是:先进行数据库迁移 Nuget控制台-1)Enable-Migrations 2)Add-Migration 3)Update-Database
你是在更新数据库?前面不是说ef连不上数据库的问题吗? 如果遇到数据库无法更新的情况,可以看看 到数据库,点开系统表,找到 __MigrationHistory 删除试试看
bakalr 2017-12-15
  • 打赏
  • 举报
回复
现在是通过这种方式解决的,版主大大说的那个连接字符串之前也使用过,也没创建起 解决办法是:先进行数据库迁移 Nuget控制台-1)Enable-Migrations 2)Add-Migration 3)Update-Database

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧