How do you create the database using code first?

c# entity-framework-6 sql-server-express

Question

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":

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

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?

1
0
12/27/2018 5:09:33 PM

Accepted Answer

Couple of things need to happen to get connectivity working:

1) Instance name of SQLEXPRESS needs to be set in the connection string.

2) Mixed mode authentication needs to be enabled on the server to allow for non-Windows domain accounts to connnect to the server.

2
12/27/2018 6:36:23 PM

Popular Answer

Please try to connect to the instance via SQL Server Management Studio with the EFLearn user and tell us whether it can connect to the instance. If not, please tell us the error message.

Could you please try to change the Server name to hostname\instancename(CPISZ1-LT\SQLEXPRESS) in the connection string.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow