您如何首先使用代码创建数据库?

How do you create the database using code first?

我正在努力学习Entity Framework。

我想先看一个代码示例。

我已经通读了以下内容link,但我觉得他们跳过了一些步骤。

http://www.entityframeworktutorial.net/code-first/simple-code-first-example.aspx

我用谷歌搜索了如何为 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

我错过了什么?

需要发生几件事才能使连接正常工作:

1) SQLEXPRESS的实例名需要在连接字符串中设置。

2) 需要在服务器上启用混合模式身份验证以允许非Windows 域帐户连接到服务器。

请尝试使用EFLearn 用户通过SQL Server Management Studio 连接到实例,并告诉我们它是否可以连接到实例。如果没有,请告诉我们错误信息。

能否尝试将连接字符串中的服务器名称更改为 hostname\instancename(CPISZ1-LT\SQLEXPRESS)。