无法在Ntier结构中创建EF数据库

时间:2019-04-14 10:53:35

标签: c# asp.net-web-api entity-framework-6 n-tier-architecture

我正在尝试将API和DataAccessLayer分开,但是我无法使用EF 6代码优先方法来创建数据库。

根据下面的结构和设置,将在localdb空间中创建一个数据库,但不会在我的计算机上的SQL Server Express实例中创建一个数据库。我想念什么或做错了什么?

项目结构:

API ( WebApi )
 - Controller
 - App.config ( from shared lib )
 - Web.config

DataAccessLayer ( Shared Library .NET Framework 461)
  - Models
  - DBContext
  - Migrations
  - web.config

Tests

API

Web.config

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  https://go.microsoft.com/fwlink/?LinkId=301879
  -->
<configuration>

  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  </configSections>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.6.1" />
    <httpRuntime targetFramework="4.6.1" />
  </system.web>
  <system.webServer>

    <modules>
      <remove name="TelemetryCorrelationHttpModule" />
      <add name="TelemetryCorrelationHttpModule" type="Microsoft.AspNet.TelemetryCorrelation.TelemetryCorrelationHttpModule, Microsoft.AspNet.TelemetryCorrelation" preCondition="integratedMode,managedHandler" />
    </modules>
    <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <remove name="TRACEVerbHandler" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f" />
        <bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Diagnostics.DiagnosticSource" publicKeyToken="cc7b13ffcd2ddd51" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.2.1" newVersion="4.0.2.1" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
        <bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="11.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.2.4.0" newVersion="5.2.4.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
    </compilers>
  </system.codedom>
  <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>
</configuration>

参考添加了DataAccessLayer.dll

控制器

 [HttpGet]
        public List<DataAccessLayer.Models.Customer> Get()
        {
            var model = new List<DataAccessLayer.Models.Customer>();
            using(var context = new DataAccessLayer.Models.DatabaseContext.CustomerTransactionContext())
            {
                model = context.Customers.ToList();
            }
            return model;
        }

DataAccessLayer

DatabaseContext

 public class CustomerTransactionContext : DbContext
    {
        public CustomerTransactionContext() : base("CustomerTransactionContext")
        {
            Database.SetInitializer(new CreateDatabaseIfNotExists<CustomerTransactionContext>());
        }

        public DbSet<Customer> Customers { get; set; }
        public DbSet<Transaction> Transactions { get; set; }
        public DbSet<CurrencyCode> CurrencyCodes { get; set; }
        public DbSet<TransactionStatus> TransactionStatuses { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }
    }

App.config -这是连接字符串部分

  <connectionStrings>
    <add name="CustomerTransactionContext"
       connectionString="Data Source=DESKTOP-6ERMBUH\SQLEXPRESS01;initial catalog=CustomerTransactionContext;Integrated Security=true"
       providerName="System.Data.SqlClient"/>
  </connectionStrings>

迁移 配置

 internal sealed class Configuration : DbMigrationsConfiguration<Models.DatabaseContext.CustomerTransactionContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
    }

    protected override void Seed(Models.DatabaseContext.CustomerTransactionContext context)
    {
        //  This method will be called after migrating to the latest version.

        //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
        //  to avoid creating duplicate seed data.

        context.Customers.AddOrUpdate(c => c.CustomerID,
           new Customer { ContactEmail = "", CustomerName = "Jonathan", MobileNo = 0123456789, CustomerID = 1 },
           new Customer { ContactEmail = "", CustomerName = "Mary", MobileNo = 424356909, CustomerID = 2 },
           new Customer { ContactEmail = "", CustomerName = "Ali", MobileNo = 612456749, CustomerID = 3 },
           new Customer { ContactEmail = "", CustomerName = "Bob", MobileNo = 716356789, CustomerID = 4 },
           new Customer { ContactEmail = "", CustomerName = "Lucy", MobileNo = 313453789, CustomerID = 5 },
           new Customer { ContactEmail = "", CustomerName = "Edwardo", MobileNo = 242346760, CustomerID = 6 }
           );

        context.TransactionStatuses.AddOrUpdate(ts => ts.TransactionStatusID,
            new TransactionStatus { Name = "Success", TransactionStatusID = 1 },
            new TransactionStatus { Name = "Failed", TransactionStatusID = 2 },
            new TransactionStatus { Name = "Canceled", TransactionStatusID = 3 });

        context.CurrencyCodes.AddOrUpdate(cc => cc.CurrencyCodeID,
           new CurrencyCode { Name = "USD", CurrencyCodeID = 1 },
           new CurrencyCode { Name = "JPY", CurrencyCodeID = 2 },
           new CurrencyCode { Name = "THB", CurrencyCodeID = 3 },
           new CurrencyCode { Name = "SGD", CurrencyCodeID = 4 });

        context.Transactions.AddOrUpdate(t => t.TransactionID,
        new Transaction { TransactionStatusID = 1, Amount = 29.99M, CurrencyCodeID = 1, TransactionDateTime = DateTime.Now, CustomerID = 1 },
        new Transaction { TransactionStatusID = 2, Amount = 9.99M, CurrencyCodeID = 3, TransactionDateTime = DateTime.Now, CustomerID = 1 },
        new Transaction { TransactionStatusID = 3, Amount = 1.32M, CurrencyCodeID = 1, TransactionDateTime = DateTime.Now, CustomerID = 1 },
        new Transaction { TransactionStatusID = 3, Amount = 1.32M, CurrencyCodeID = 1, TransactionDateTime = DateTime.Now, CustomerID = 1 },
        new Transaction { TransactionStatusID = 3, Amount = 1.32M, CurrencyCodeID = 1, TransactionDateTime = DateTime.Now, CustomerID = 1 });
    }
}

2 个答案:

答案 0 :(得分:1)

Web.config中查看以下几行:

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

您的连接工厂设置为mssqllocaldb,这就是使用本地数据库创建数据库的原因。修复您的连接字符串以使用您的SQL实例名称,或者只是删除此代码以使App.config设置起作用。

答案 1 :(得分:0)

这是在dbcontext类中描述您的连接字符串的好方法,例如

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    if (!optionsBuilder.IsConfigured)
    {                
        optionsBuilder.UseSqlServer(@"Server=Servername;Database=Dbname;User ID=sa;Password=1234qqqQ;TrustServerCertificate=True;Trusted_Connection=False;Connection Timeout=30;Integrated Security=False;Persist Security Info=False;Encrypt=True;MultipleActiveResultSets=True;");
    }
}

这使您能够将迁移添加到数据库中。其次,您的应用程序应该知道它将进入哪个数据库。在.net core中,添加以下代码startup.cs

  var connection = @"Server=Servername;Database=Dbname;User ID=sa;Password=1234qqqQ;TrustServerCertificate=True;Trusted_Connection=False;Connection Timeout=30;Integrated Security=False;Persist Security Info=False;Encrypt=True;MultipleActiveResultSets=True;";
        services.AddDbContext<BeautyCenterContext>(options => options.UseSqlServer(connection)); 

在pm中添加迁移时,不要忘记将默认项目更改为包含上下文类的项目。希望这会有所帮助。