WinForms和EntityFramework [英] WinForms and EntityFramework

查看:75
本文介绍了WinForms和EntityFramework的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,当前打开第三方数据源,提取数据,做一些ETL,以及T大部分。然后,应用程序将数据存储在本地数据库(LocalDB)中,我将其存储在此处,因为我们在非洲的网络连接可能很慢或不存在。

I have an application that currently opens up a 3rd party DataSource that pulls the data, does some ETL, well the T mostly. The application then stores the data in a local database (LocalDB), I store it here since our network connections in Africa can be quite slow or non-existent.

一旦我拥有我的LocalDB上的数据我在MVC项目上调用WebAPI,然后将所有数据保存在云中托管的数据库中。为了便于携带,我正忙着从拇指驱动器运行它。我希望数据保存到它,但它一直保存
到用户的Documents文件夹。

Once I have the data on my LocalDB I call a WebAPI on an MVC project that then saves all the data on that database hosted in the cloud. For portability, I am busy getting it to run from a thumb drive. I want the data to save to it, but it keeps saving to the user's Documents folder.

当我的App.config中没有连接字符串时,EF会完美地生成数据库,在需要时删除它。一旦我将连接字符串放在我的App.config中,就会一直打破"错误:50 - 发生本地数据库运行时错误。指定的LocalDB
实例名称无效。"

When I have no connection string in my App.config EF generates the database perfectly, deletes it when needs to. Once I put the connection string in my App.config it keeps breaking with "error: 50 - Local Database Runtime error occurred. Specified LocalDB instance name in invalid."

案例1:

  <connectionStrings>
    <add name="LocalDBContext" connectionString="
         Data Source=(localdb)\\mssqllocaldb;
         Initial Catalog=PSIologist.LocalDBContext;
         Integrated Security=True;MultipleActiveResultSets=True"
         
         providerName="System.Data.SqlClient" />
  </connectionStrings>
  

错误50。

案例2:

  <connectionStrings>
    <add name="LocalDBContext" connectionString="
         AttachDbFilename=|DataDirectory|\PSI-Data.mdf;
         Data Source=(localdb)\\mssqllocaldb;
         Initial Catalog=PSIologist.LocalDBContext;
         Integrated Security=True;MultipleActiveResultSets=True"
         
         providerName="System.Data.SqlClient" />
  </connectionStrings>

错误50。

执行以下代码后会发生这种情况

This happens after the following code gets executed

            using (LocalDBContext db = new LocalDBContext())
            {
                if (db.Database.Exists()) 
                    db.Database.Delete();
                
                try
                {                
                    db.Database.CreateIfNotExists();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

                if (db.Database.Exists()) MessageBox.Show("DB reset complete"); // employees "+count);
                else MessageBox.Show("DB reset failed");          
            }  

知道我在这个简单的东西上缺少什么吗?

Any idea what I am missing on something this simple?

推荐答案

您的服务器名称是对于LocalDB不正确,如果这是您正在尝试使用的。 LocalDB使用(localdb)\v {version}语法进行连接。请参阅LocalDB
此处周围的小节,以获取您尝试使用的变体的正确连接字符串。

Your server name is not correct for LocalDB, if that is what you're trying to use. LocalDB uses (localdb)\v{version} syntax for connecting. Refer to the subsections around LocalDB here to get the correct connection string for the variant you're trying to use.

请注意,当数据库也是解决方案中的项目时,DB实际上是以多个名称发布的。看起来您可能正在尝试使用项目名称而不是LocalDB实例名称。

Note that the DB is actually published under several names when the database is also a project in your solution. It appears you might be trying to use the project name instead of the LocalDB instance name.

Michael Taylor

http://www.michaeltaylorp3.net

Michael Taylor
http://www.michaeltaylorp3.net


这篇关于WinForms和EntityFramework的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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