无法从实体框架生成数据库4.1 Codefirst与SQL Server 2005 [英] Cannot Generate Database from Entity Framework 4.1 Codefirst With SQL Server 2005

查看:127
本文介绍了无法从实体框架生成数据库4.1 Codefirst与SQL Server 2005的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从实体框架代码创建数据库,首先遵循本教程

I try to create database from entity framework code first follow with this tutorial

http://www.asp.net/mvc/tutorials/getting-started-with-mvc3-part4-cs

但是我使用SQL Server 2005而不是SQLExpress,但当我执行解决方案没有任何创建。我的代码有什么问题

but I use SQL Server 2005 instead when SQLExpress but When I execute solution don't have anything create. What wrong with my code

这是我的代码。

Movie.cs

public class Movie
{
    public int ID { get; set; }
    public string Title { get; set; }
    public DateTime ReleaseDate { get; set; }
    public string Genre { get; set; }
    public decimal Price { get; set; }
}

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

这是我的web.config中的连接字符串

And this is my connection string in web.config

<add name="MovieDBContext" 
     connectionString="data source=...;Integrated Security=SSPI;initial catalog=MovieDB;User Id=...;Password=..."
     providerName="System.Data.SqlClient" />

我的代码错了什么?为什么没有创建数据库。

What Wrong with my code? Why database wasn't created.

谢谢每一个人帮助我。

推荐答案

数据库在第一次使用之前不会创建。您必须执行以下操作才能触发数据库创建:

The database is not created until it is used for the first time. You must do any of following to trigger database creation:


  • 创建上下文的实例并检索或保留任何数据

  • 创建上下文的实例并调用 context.Database.CreateIfNotExists()

  • 创建上下文的实例并调用 context.Database.Initialize(false)

  • 创建上下文的实例,并调用 context.Database.Create ()

  • Create instance of your context and retrieve or persist any data
  • Create instance of your context and call context.Database.CreateIfNotExists()
  • Create instance of your context and call context.Database.Initialize(false)
  • Create instance of your context and call context.Database.Create()

这篇关于无法从实体框架生成数据库4.1 Codefirst与SQL Server 2005的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆