新人,最近在学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层和其他
但是直接初始化数据库的时候出现了这种错误
求大神指教