bakalr 2017-12-15 00:56 采纳率: 0%
浏览 1292
已结题

菜鸟求问!在使用EF Code First模式下创建数据库连接失败的问题

新人,最近在学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>

没有写view层和其他
但是直接初始化数据库的时候出现了这种错误
图片说明

求大神指教

  • 写回答

5条回答

  • 聂14昊51 2017-12-15 01:07
    关注

    我看了你的代码,问题不是很大,你用数据库客户端登陆sa能登陆上么,你要先确保你的数据库本身是可以登陆上的,排除数据库配置问题。

    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)