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

时间:2018-12-24 16:49:28

标签: c# entity-framework-6 sql-server-express

我正在尝试学习实体框架。

我想先遵循一个代码示例。

我已经阅读了以下链接中的所有内容,但我觉得他们跳过了一些步骤。

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

我搜索了如何使用户成为sql-express的人。我用密码“ EFLearn”创建了一个名为“ EFLearn”的文件,我对创建数据库的方法也做了同样的事情,我在“ EFLearnDB”上创建了该文件:

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

我查找了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

我想念什么?

2 个答案:

答案 0 :(得分:2)

要使连接正常工作,需要发生一系列事情:

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

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

答案 1 :(得分:0)

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

能否请您尝试在连接字符串中将服务器名称更改为主机名\实例名(CPISZ1-LT \ SQLEXPRESS)。