【发布时间】:2018-12-24 16:49:27
【问题描述】:
我正在努力学习实体框架。
我想遵循一个代码优先的例子。
我已经阅读了以下链接上的所有内容,但我觉得他们跳过了一些步骤。
http://www.entityframeworktutorial.net/code-first/simple-code-first-example.aspx
我在 Google 上搜索了如何为 sql-express 创建用户。我用密码“EFLearn”创建了一个名为“EFLearn”的数据库,我对如何创建数据库做了同样的事情,并在名为“EFLearnDB”上创建了一个:
我查看了 Sql Express 的连接字符串是什么样的,并将其放入我的应用配置中。
我创建了我的解决方案和一些类来表示我的数据,以及我的上下文,如下所示:
namespace EFLearning {
class Program {
static void Main(string[] args) {
using (var manufacturingDbContext = new ManufacturingDbContext()) {
var good = new Good() { Name = "Water" };
manufacturingDbContext.Goods.Add(good);
manufacturingDbContext.SaveChanges();
}
}
}
}
namespace Domain {
public class ManufacturingDbContext :DbContext {
public DbSet<Good> Goods { get; set; }
public DbSet<Manufacturer> Manufacturers { get; set; }
public ManufacturingDbContext() : base("name=EFLearnConnectionString") {
}
}
}
namespace Domain
{
public class Good
{
public int Id { get; set; }
public string Name { get; set; }
public double BaseValue { get; set; }
}
}
namespace Domain {
public class Manufacturer {
public int Id {get; set;}
public ICollection<ManufacturingCapability> ManufacturingCapabilities { get; set; }
}
}
namespace Domain {
public class ManufacturingCapability {
public int Id { get; set; }
public ICollection<ManufacturingInput> Inputs { get; set; }
public ICollection<ManufacturingOutput> Outputs { get; set; }
public TimeSpan CycleTime { get; set; }
}
}
namespace Domain {
public class ManufacturingInput {
public int Id { get; set; }
public Good Good { get; set; }
public uint Quantity { get; set; }
}
}
namespace Domain {
public class ManufacturingOutput {
public int Id { get; set; }
public Good Good { get; set; }
public uint Quantity { get; set; }
}
}
我有以下 AppConfig:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<connectionStrings>
<add name="EFLearnConnectionString"
connectionString="Server=localhost;Database=EFLearnDb;User Id=EFLearn;Password=EFLearn;"
providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
我运行程序并希望创建表。但是,相反,我得到以下异常:
+ $exception {"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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)"} System.Data.SqlClient.SqlException
我错过了什么?
【问题讨论】:
-
我相信我创建了一个名为“EFLearnDB”的数据库——对于您不确定的事情,我们真的无能为力。你有没有?使用 SQL Server Management Studio 来解决这个问题。还要检查
Server=localhost是否正确。我希望那里有一个实例名称,例如Server=(local)\sqlexpress。 -
@Gert 当然。它就在那里。我使用 Sql Management Studio 来创建它。向 OP 添加了屏幕截图。有趣的是,我也无法通过服务器资源管理器在 Visual Studio 中连接到它,使用 MS SQL Server 进行数据源选择,使用 .NET Data Provider for SQL Server。当它寻找服务器时,它会出现空白。
-
如果我按照您的描述将连接字符串更改为使用 (local)\scqlexpress,异常会更改为登录失败异常,而不是无法连接。进步。我将添加我创建的用户属性的屏幕截图。
-
您在 app.config 中的密码拼写为“EfLearn”,但在帖子顶部的措辞中,它是“EFLearn”(大写 F)。
-
@user 是的,我抓住了这一点并尝试使用大写字母。仍然登录失败。
标签: c# entity-framework-6 sql-server-express