简体   繁体   English

ASP.NET从任何地方检索数据

[英]ASP.NET retrieving data from nowhere

I am going through ASP.NET MVC 3 tutorial . 我正在阅读ASP.NET MVC 3教程 Got to the point where EnityFramework was used for model classes . 到了将EnityFramework用于模型类的地步。 I ran the the application without having connection strings in the web.config specified and it worked. 我在没有指定web.config中的连接字符串的情况下运行了该应用程序,并且该应用程序正常运行。

I could add, edit, delete records. 我可以添加,编辑,删除记录。 And the weirdest thing is that they are sill there even after I stop development server and start debugging application again as if table was created somewhere in memory and stayed alive for some reason. 最奇怪的是,即使我停止了开发服务器并再次开始调试应用程序之后,它们仍然在那里,好像表是在内存中的某个位置创建的,并且由于某种原因而保持活动状态一样。 Can anybody explain what was going on? 有人可以解释发生了什么吗?

Here's a link to an image: oi48.tinypic.com/fnbeba.jpg 这是图像的链接:oi48.tinypic.com/fnbeba.jpg

Was it supposed to use the the connection string for which name matches the name of the class derived from DbContext? 是否应该使用名称与从DbContext派生的类的名称匹配的连接字符串?

EDIT1: 编辑1:
Because I had no connection string in web.config it was generated by Enity Framework using namespace and the name of the class derived from DBContext. 因为在web.config中没有连接字符串,所以它是由Enity Framework使用名称空间和从DBC​​ontext派生的类的名称生成的。 By default EF uses SQL Express, hence database file was created in database server's DATA direcotry C:\\Program Files\\Microsoft SQL Server\\MSSQL10_50.SQLEXPRESS\\MSSQL\\DATA. 默认情况下,EF使用SQL Express,因此在数据库服务器的DATA目录中创建了数据库文件C:\\ Program Files \\ Microsoft SQL Server \\ MSSQL10_50.SQLEXPRESS \\ MSSQL \\ DATA。

The last thing I don't understand why it wouldn't create the database file in App_Data dir if DBContext derived class is 最后一件事我不明白,如果DBContext派生的类是为什么它不会在App_Data dir中创建数据库文件

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

    // Passing name of the connections string shouldn't be even necessary
    public MoviesDBContex()
        : base("MoviesDBContex")
    { }
}

and web.config contains 和web.config包含

  <connectionStrings>
    <add name="MoviesDBContext"
      connectionString="Data Source=|DataDirectory|Movies.sdf"
      providerName="System.Data.SqlClient"/>
  </connectionStrings>

Thist what the Microsoft guide about ER connections and models (msdn.microsoft.com/en-us/data/jj592674.aspx) says: 这就是Microsoft关于ER连接和模型的指南(msdn.microsoft.com/en-us/data/jj592674.aspx)所说的:

If the name of the connection string matches the name of your context (either with or without namespace qualification) then it will be found by DbContext when the parameterless constructor is used. 如果连接字符串的名称与您的上下文的名称匹配(具有或不具有名称空间限定),则在使用无参数构造函数时,DbContext会找到它。 If the connection string name is different from the name of your context then you can tell DbContext to use this connection in Code First mode by passing the connection string name to the DbContext constructor. 如果连接字符串名称与您的上下文名称不同,则可以通过将连接字符串名称传递给DbContext构造函数来告诉DbContext在代码优先模式下使用此连接。

Any idea why database file isn't created in App_Data? 知道为什么未在App_Data中创建数据库文件吗?

EDIT2: 编辑2:
There was a missing 't' in the class name and hence it didn't match the name of the connection string. 类名中缺少“ t”,因此与连接字符串的名称不匹配。

I does work as expected when provider is SQL Server Compact providerName="System.Data.SqlServerCe.4.0", but if I change it to providerName="System.Data.SqlClient" I get an exception added below. 当提供程序为SQL Server Compact providerName =“ System.Data.SqlServerCe.4.0”时,我确实按预期工作,但是如果将其更改为providerName =“ System.Data.SqlClient”,则会在下面添加异常。 Shouldn't regular SQL Server be able to create a database file also? 常规SQL Server是否也不能创建数据库文件?

A network-related or instance-specific error occurred while establishing a connection to SQL Server. 建立与SQL Server的连接时发生与网络相关或特定于实例的错误。 The server was not found or was not accessible. 服务器未找到或无法访问。 Verify that the instance name is correct and that SQL Server is configured to allow remote connections. 验证实例名称正确,并且已将SQL Server配置为允许远程连接。 (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (提供者:命名管道提供程序,错误:40-无法打开与SQL Server的连接)

Description: An unhandled exception occurred during the execution of the current web request. 说明:执行当前Web请求期间发生未处理的异常。 Please review the stack trace for more information about the error and where it originated in the code. 请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息。

我相信它正在使用SQL Server Express,将数据库文件写入应用程序内的app_data目录。

Do you have SQL Express installed on your machine? 您的计算机上安装了SQL Express吗? Which version of Entity Framework are you using? 您正在使用哪个版本的Entity Framework? As long as I know the Entity Framework targets the local SQL Express as its default connection string if no string connection is provided. 只要我知道,如果没有提供字符串连接,则实体框架会将本地SQL Express作为其默认连接字符串。

This is called "Convention over configuration", and Entity Framework and MVC use this principle a lot. 这被称为“配置约定”,而Entity Framework和MVC经常使用此原理。 Basically, it says that unless you give it specific instructions otherwise, they will assume various conventions. 基本上,它说,除非另有说明,否则它们将采用各种约定。

If you don't supply a connection string to EF, then it will use a default connection string, generated from the namespeace and name of the class. 如果不向EF提供连接字符串,则它将使用默认的连接字符串,该字符串是从类的名称和名称生成的。 If that doesn't exist in your web.config, then it will use the DefaultConnection that is defined in the machine.config located in the folder tree at C:\\Windows\\Microsoft.NET 如果您的web.config中不存在该文件,则它将使用位于C:\\ Windows \\ Microsoft.NET文件夹树中的machine.config中定义的DefaultConnection。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM