I am trying to learn Entity Framework.
I'd like to follow a code first example.
I've read over everything on the following link, but I feel like they skipped over some steps.
http://www.entityframeworktutorial.net/code-first/simple-code-first-example.aspx
I googled how to make a user for sql-express. I created one called "EFLearn" with password "EFLearn" I did the same with how to create the database and I created on called "EFLearnDB":
I looked up what a connection string for Sql Express looks like and put it in my app config.
I created my solution and some classes to represent my data, along with my context as follows:
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; }
}
}
I've got the following 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>
I run the program and I expect tables to be created. However, instead, I get the following exception:
+ $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
What am I missing?
Server=localhost
is correct. I'd expect an instance name there, likeServer=(local)\sqlexpress
.