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 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100