简体   繁体   English

实体框架代码优先方法获取“System.InvalidOperationException”错误

[英]Enitity Framework code-first aproach getting "System.InvalidOperationException" error

I have been following this tutorial我一直在关注这个教程

And I keep getting this "Enitity Framework code first aproach getting "System.InvalidOperationException" error" on the Movie controller.我一直在电影控制器上收到这个“实体框架代码优先获取“System.InvalidOperationException”错误的方法。

I have been looking trough the other solutions but they don't seem to help me.我一直在寻找其他解决方案,但它们似乎对我没有帮助。

This is my Cinemacontext class.这是我的 Cinemacontext 类。

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>();
        }
    }
}

Initializer初始化程序

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();
        }
    }
}

Movie Modal.电影模态。

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 from web.config :来自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>

Movie controller电影控制器

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());
        }   
    }
}

Does anyone got an idea what is wrong with my code?有没有人知道我的代码有什么问题?

You need to change this <context type="Cinema.DAL.CinemaContexts, Cinema"> to <context type="Cinema.DAL.CinemaContext, Cinema"> to match the name you used here:您需要将此<context type="Cinema.DAL.CinemaContexts, Cinema">更改为<context type="Cinema.DAL.CinemaContext, Cinema">以匹配您在此处使用的名称:

public CinemaContext() : base("CinemaContext")
        {
        }

Hope it will help!希望它会有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有数据库优先实体框架错误System.InvalidOperationException的MVC身份 - MVC identity with database first Entity Framework Error System.InvalidOperationException EF代码优先-System.InvalidOperationException - EF Code First - System.InvalidOperationException 收到错误-未处理System.InvalidOperationException - getting error - System.InvalidOperationException was unhandled 实体框架System.InvalidOperationException - Entity Framework System.InvalidOperationException 代码抛出System.InvalidOperationException - Code throws System.InvalidOperationException 出现错误System.InvalidOperationException:反序列化JSON凭证数据时出错 - getting error System.InvalidOperationException: Error deserializing JSON credential data DropDownList 错误,System.InvalidOperationException - DropDownList Error, System.InvalidOperationException System.InvalidOperationException 错误原因? - System.InvalidOperationException error reason? C#WPF获取错误 - System.InvalidOperationException: - C# WPF Getting error - System.InvalidOperationException: 获取错误:System.InvalidOperationException:用户未处理 - Getting Error : System.InvalidOperationException: User-Unhandled
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM