代码优先不起作用

时间:2017-06-07 11:48:00

标签: c# asp.net asp.net-mvc entity-framework model-view-controller

我首先尝试添加代码,但App_Data文件夹为空。

的Web.config

<connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-Movie-20170604111514.mdf;Initial Catalog=aspnet-Movie-20170604111514;Integrated Security=True"
          providerName="System.Data.SqlClient" />
    <add name="MovieDBContext" connectionString="Data Source=(localDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True"
          providerName="System.Data.SqlClient" /> 
</connectionStrings>

模型

public class MovieDBContext : DbContext
{
    public DbSet<Movie> Movies { get; set; }    
}

任何人都可以帮助我吗?提前谢谢。

1 个答案:

答案 0 :(得分:1)

您需要在 MovieDBContext 类中添加构造函数,如下所示:

public class MovieDBContext : DbContext
{
    public MovieDBContext () : base("Name=MovieDBContext")
    {
    }

    public DbSet<Movie> Movies { get; set; }    
}

有关详细信息,请阅读以下链接: http://www.entityframeworktutorial.net/code-first/entity-framework-code-first.aspx