实体框架代码优先方法出现 "System.InvalidOperationException" 错误
Enitity Framework code-first aproach getting "System.InvalidOperationException" error
我一直在关注this tutorial
我一直在电影控制器上收到此 "Enitity Framework code first aproach getting "System.InvalidOperationException“错误”。
我一直在寻找其他解决方案,但它们似乎对我没有帮助。
这是我的 Cinemacontext class。
using Cinema.Models;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
namespace Cinema.DAL
{
public class CinemaContext : DbContext
{
public CinemaContext() : base("CinemaContext")
{
}
public DbSet<Movie> Movies { get; set; }
public DbSet<Room> Rooms { get; set; }
public DbSet<Visitor> Visitors { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
}
初始化程序
using Cinema.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Cinema.DAL
{
public class CinemaInitializer : System.Data.Entity.DropCreateDatabaseIfModelChanges<CinemaContext>
{
protected override void Seed(CinemaContext context)
{
var movies = new List<Movie>
{
new Movie{Name="Starwars",StartTime=DateTime.Parse("2020-01-31"),Duration=1},
new Movie{Name="lotr",StartTime=DateTime.Parse("1995-08-12"),Duration=1},
new Movie{Name="hobbit",StartTime=DateTime.Parse("2014-03-22"),Duration=1},
new Movie{Name="spongebob",StartTime=DateTime.Parse("2003-05-06"),Duration=1},
new Movie{Name="holygrail",StartTime=DateTime.Parse("1969-01-31"),Duration=1},
};
movies.ForEach(s => context.Movies.Add(s));
context.SaveChanges();
}
}
}
电影模式。
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
namespace Cinema.Models
{
public class Movie
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime? StartTime { get; set; }
public int Duration { get; set; }
}
}
Entity Framework 来自 web.config
:
<entityFramework>
<contexts>
<context type="Cinema.DAL.CinemaContexts, Cinema">
<databaseInitializer type="Cinema.DAL.CinemaInitializer, Cinema" />
</context>
</contexts>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
电影控制器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Cinema.DAL;
using Cinema.Models;
namespace Cinema.Controllers
{
public class MovieController : Controller
{
public DAL.CinemaContext db { get; set; } = new DAL.CinemaContext();
public ViewResult MovieList()
{
return View(db.Movies.ToList());
}
}
}
有人知道我的代码有什么问题吗?
您需要将此 <context type="Cinema.DAL.CinemaContexts, Cinema">
更改为 <context type="Cinema.DAL.CinemaContext, Cinema">
以匹配您在此处使用的名称:
public CinemaContext() : base("CinemaContext")
{
}
希望对您有所帮助!
我一直在关注this tutorial
我一直在电影控制器上收到此 "Enitity Framework code first aproach getting "System.InvalidOperationException“错误”。
我一直在寻找其他解决方案,但它们似乎对我没有帮助。
这是我的 Cinemacontext class。
using Cinema.Models;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
namespace Cinema.DAL
{
public class CinemaContext : DbContext
{
public CinemaContext() : base("CinemaContext")
{
}
public DbSet<Movie> Movies { get; set; }
public DbSet<Room> Rooms { get; set; }
public DbSet<Visitor> Visitors { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
}
初始化程序
using Cinema.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Cinema.DAL
{
public class CinemaInitializer : System.Data.Entity.DropCreateDatabaseIfModelChanges<CinemaContext>
{
protected override void Seed(CinemaContext context)
{
var movies = new List<Movie>
{
new Movie{Name="Starwars",StartTime=DateTime.Parse("2020-01-31"),Duration=1},
new Movie{Name="lotr",StartTime=DateTime.Parse("1995-08-12"),Duration=1},
new Movie{Name="hobbit",StartTime=DateTime.Parse("2014-03-22"),Duration=1},
new Movie{Name="spongebob",StartTime=DateTime.Parse("2003-05-06"),Duration=1},
new Movie{Name="holygrail",StartTime=DateTime.Parse("1969-01-31"),Duration=1},
};
movies.ForEach(s => context.Movies.Add(s));
context.SaveChanges();
}
}
}
电影模式。
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
namespace Cinema.Models
{
public class Movie
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime? StartTime { get; set; }
public int Duration { get; set; }
}
}
Entity Framework 来自 web.config
:
<entityFramework>
<contexts>
<context type="Cinema.DAL.CinemaContexts, Cinema">
<databaseInitializer type="Cinema.DAL.CinemaInitializer, Cinema" />
</context>
</contexts>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
电影控制器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Cinema.DAL;
using Cinema.Models;
namespace Cinema.Controllers
{
public class MovieController : Controller
{
public DAL.CinemaContext db { get; set; } = new DAL.CinemaContext();
public ViewResult MovieList()
{
return View(db.Movies.ToList());
}
}
}
有人知道我的代码有什么问题吗?
您需要将此 <context type="Cinema.DAL.CinemaContexts, Cinema">
更改为 <context type="Cinema.DAL.CinemaContext, Cinema">
以匹配您在此处使用的名称:
public CinemaContext() : base("CinemaContext")
{
}
希望对您有所帮助!