实体框架抛出异常 - 网络相关的或特定于实例出错 [英] Entity Framework throws exception - Network Related or instance-specific error occurred

查看:102
本文介绍了实体框架抛出异常 - 网络相关的或特定于实例出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我跟着MVC 3的教程,并做到了这一点:

I've followed a tutorial in MVC 3, and done this:


用户 [有] 网​​站中的名单[有]的页面列表 S [有] 注释列表[每个标记的单词是一类自身的。

Made 4 classes and a DBClass:
User [has a] List of Websites [has a] List of Pages [has a] List of Comments [each marked word is a class of its own].

然后,我做了一个名为 UserDataBaseDB 这样的类:

Then, I made a class called UserDataBaseDB this way:

public partial class UserDataBaseDB : DbContext
    {
        public DbSet<User> Users { get; set; }

        public UserDataBaseDB()
            : base(@"UserDataBaseDB")
        {
            if (this.Database.Exists())
            {
                try
                {
                    this.Database.CompatibleWithModel(false);
                }
                catch (InvalidOperationException)
                {
                    this.Database.Delete();
                }
            }

            if (!this.Database.Exists())
            {
                this.Database.Create();
                Seed();
            }
        }

        private void Seed()
        {
            var Mark = new User();
            Mark.ID = 0;
            Mark.MD5Pass = "4297f44b13955235245b2497399d7a93";
            Mark.UserName = "Admin";

            var Troll = new Comment();
            Troll.CommentData = "Fake!";
            Troll.Writer = Mark;

            var Site = new Website();
            Site.Name = "Admin Site";

            Site.Pages.ElementAt(0).Data = "Awesome website!";
            Site.Pages.ElementAt(0).Comments.Add(Troll);

            Mark.Websites.Add(Site);

            Users.Add(Mark);
        }
        public static UserDataBaseDB Create()
        {
            return new UserDataBaseDB();
        }
        public User FindUserByID(int id)
        {
            return (from item in this.Users
                    where item.ID == id
                    select item).SingleOrDefault();
        }

然后,我添加了控制器查看,并导航到的http://本地主机:40636 /主,我看到这条消息:

Then I added the Controller and View, and navigated to http://localhost:40636/Main, and I saw this message:

建立SQL Server的连接时发生网络相关的或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确,以及SQL Server配置为允许远程连接。 (提供者:SQL网络接口,错误:26 - 错误定位指定的服务器/实例)

A network-related or instance-specific error occurred while establishing a connection to 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. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

Line 14:             : base(@"UserDataBaseDB")
Line 15:         {
Line 16:             **if (this.Database.Exists())**
Line 17:             {
Line 18:                 try

放生标线是红线。

Letting the marked line be the red line.

我试着重新安装实体框架,还试图EF codeFirst,试图使我自己的 SqlDataBase ,并给予它的连接字符串,但是毫无效果。

I've tried to re-install Entity Framework, also trying EFCodeFirst, tried making my own SqlDataBase and giving it the connection string, but nothing worked.

有谁知道有什么解决办法?谢谢!

Does anyone know what's the solution? Thank you!

PS:我使用的是64位的Win7机器上VS旗舰版SP1

PS: I'm using VS Ultimate SP1 on a Win7 64bit machine.

推荐答案

这可能是一个连接字符串的问题,然而,我看到你试图种子信息,完全在构造函数中,我会强烈建议使用上下文你不这样做。
您可以实现OnModelCreating方法,并尝试你的初始化code那里,甚至更好的在应用程序的开始。它只是似乎没有权利停留在构造函数初始化刚刚开始在这一点上。

This is likely a connect string issue, however I do see that you are trying to seed information and fully use the context in the constructor, which I would highly recommend you dont do. You can implement the OnModelCreating method and attempt your initialization code there, or even better in your application start. It just doesnt seem right to be stuck in the constructor as initialization is just starting at this point.

设置上


 if (this.Database.Exists())

是否错误行后立即发生?如果是这样,三重检查您的连接字符串。你有没有在一个项目中的code或爆发?请确保你正在检查你的根Web项目的web.config连接字符串,而不是任何组件的app.config中,如果你这么碰巧使用该设计。

Does the error happen immediately after that line? If so, triple check your connect string. Do you have your code in one project, or broken out? Make sure you are checking the connect string in your root web project's web.config and not any component's app.config if you so happen to be using that design.

每个从code-第一,等你改变你的连接字符串修改时间?一个code-首先连接字符串的上下文类的名字命名,是简单的:

Each time you changes from code-first, etc did you change your connect string? A code-first connect string is named after your context class and is simple:



<add name="UserDataBaseDB" connectionString="Data Source=|DataDirectory|test.sdf" providerName="System.Data.SqlServerCe.4.0" /%gt;

这是使用SQLCe4当然是。 SQL Server的将是

That is of course using SQLCe4. Sql Server would be



<add name="UserDataBaseDB" providerName="System.Data.SqlClient" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=YourDbName;Integrated Security=True;Pooling=False" /%gt;

或在APP_DATA分贝

or for a db in app_Data



<add name="UserDataBaseDB" providerName="System.Data.SqlClient" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\YourDatabase.mdf;User Instance=true" /%gt;

这篇关于实体框架抛出异常 - 网络相关的或特定于实例出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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