2017-11-03 13 views
1

私はまずEF 6.1.2を使って自分のアプリをコードで書きました。コマンド "Update Database"を入力した後、データベースはMicrosoft SQL Server 2017で作成されました。しかし、私はapp.config(クラスライブラリから)のデータベースに接続文字列を見たことはありません。それだけではありませんが、驚いたことに私にとってはうまくいきます。私のローカルサーバーに完璧に接続します。最初にEFコードで「見えない」接続文字列を上書きする方法は?

今、私はデータベースをクラウドに移動しました。したがって、接続文字列があります。私はアプリケーション設定に自分の接続文字列をコピーして貼り付けましたが、まだ初期のサーバを指しています。

私はapp.configは、次のようになります。

<?xml version="1.0" encoding="utf-8"?> 
    <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> 
     <entityFramework> 
     <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /> 
     <providers> 
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 
     </providers> 
     </entityFramework> 
    </configuration> 

I tried putting the connection string but it takes no effect. It still points to the local server. I have even deleted everything, and pasted my connection string but no success. 

    Any ideas of how to add my connection string so that it will point to the database from the cloud? 

    **UPDATE**: 
The web.config as Roman requested. I have put the connection string there but it makes no difference (yes I am am aware of the 'X'-s, I don't wanna share the database :-)): 

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <configSections> 
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
    </configSections> 

    <connectionStrings> 
    <add name="freeaspconnection" connectionString="Server=XXXX;Database=XXXXXX;uid=liviusosu;pwd=;" providerName="System.Data.SqlClient" /> 
    </connectionStrings> 

    <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" /> 
    <add key="NumberOfUserTasksPerPage" value="7" /> 
    <add key="NumberOfArticlesPerPage" value="10" /> 
    <add key="NumberOfCoursesPerPage" value="10" /> 
    <add key="NumberOfJobAnnouncementsPerPage" value="30" /> 
    <add key="NumberOfInterviewQuestionsPerPage" value="30" /> 
    </appSettings> 
    <system.web> 
    <compilation debug="true" targetFramework="4.5" /> 
    <httpRuntime targetFramework="4.5" maxRequestLength="1048576" /> 
    <authentication mode="Forms"> 
     <forms loginUrl="User/LogIn" timeout="300" /> 
    </authentication> 
    </system.web> 
    <runtime> 
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
     <dependentAssembly> 
     <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" /> 
     <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.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.5.2.14234" newVersion="1.5.2.14234" /> 
     </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="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.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=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" /> 
     <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" /> 
    </compilers> 
    </system.codedom> 
    <entityFramework> 
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /> 
    <providers> 
     <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 
    </providers> 
    </entityFramework> 

</configuration> 

そして、ここではDBContext

public class CareerTrackContext : DbContext 
{ 
    public CareerTrackContext() 
     : base("CareerTrack") 
    { 

    } 

    public DbSet<User> Users { get; set; } 
    public DbSet<Article> Articles { get; set; } 
    public DbSet<UserTask> UserTasks { get; set; } 
    public DbSet<Role> Roles { get; set; } 
    public DbSet<Company> Companies { get; set; } 
    public DbSet<Course> Courses { get; set; } 
    public DbSet<Book> Books { get; set; } 
    public DbSet<Review> Reviews { get; set; } 
    public DbSet<Location> Locations { get; set; } 
    public DbSet<JobAnnouncement> JobAnnouncements { get; set; } 
    public DbSet<Skill> Skills { get; set; } 
    public DbSet<InterviewQuestion> InterviewQuestions { get; set; } 

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

     modelBuilder.Properties() 
      .Where(p => p.Name == p.ReflectedType.Name + "Id") 
      .Configure(p => p.IsKey()); 

     modelBuilder.Properties<string>() 
      .Configure(p => p.HasColumnType("varchar")); 

     modelBuilder.Properties<string>() 
      .Configure(p => p.HasMaxLength(1000)); 

     modelBuilder.Entity<JobAnnouncement>() 
      .HasMany(c => c.Skills).WithMany(i => i.JobAnnouncements) 
      .Map(t => t.MapLeftKey("JobAnnouncementId") 
       .MapRightKey("SkillId") 
       .ToTable("JobAddsSkills")); 

     modelBuilder.Configurations.Add(new UserConfiguration()); 
     modelBuilder.Configurations.Add(new ArticleConfiguration()); 
     modelBuilder.Configurations.Add(new UserTaskConfiguration()); 
     modelBuilder.Configurations.Add(new RoleConfiguration()); 
     modelBuilder.Configurations.Add(new CompanyConfiguration()); 
     modelBuilder.Configurations.Add(new CourseConfiguration()); 
     modelBuilder.Configurations.Add(new BookConfiguration()); 
     modelBuilder.Configurations.Add(new ReviewConfiguration()); 
     modelBuilder.Configurations.Add(new LocationConfiguration()); 
     modelBuilder.Configurations.Add(new JobAnnouncementConfiguration()); 
     modelBuilder.Configurations.Add(new SkillConfiguration()); 
     modelBuilder.Configurations.Add(new IntreviewQuestionConfiguration()); 

     base.OnModelCreating(modelBuilder); 
    } 

    public override int SaveChanges() 
    { 
     foreach (var entry in ChangeTracker.Entries().Where(entry => entry.Entity.GetType().GetProperty("DateRegistered") != null)) 
     { 
      if (entry.State == EntityState.Added) 
      { 
       entry.Property("DateRegistered").CurrentValue = DateTime.Now; 
      } 

      if (entry.State == EntityState.Modified) 
      { 
       entry.Property("DateRegistered").IsModified = false; 
      } 
     } 

     foreach (var entry in ChangeTracker.Entries().Where(entry => entry.Entity.GetType().GetProperty("DateAdded") != null)) 
     { 
      if (entry.State == EntityState.Added) 
      { 
       entry.Property("DateAdded").CurrentValue = DateTime.Now; 
      } 

      if (entry.State == EntityState.Modified) 
      { 
       entry.Property("DateAdded").IsModified = false; 
      } 
     } 

     foreach (var entry in ChangeTracker.Entries().Where(entry => entry.Entity.GetType().GetProperty("IsActive") != null)) 
     { 
      if (entry.State == EntityState.Added) 
      { 
       entry.Property("IsActive").CurrentValue = true; 
      } 
     } 

     foreach (var entry in ChangeTracker.Entries().Where(entry => entry.Entity.GetType().GetProperty("IsRead") != null)) 
     { 
      if (entry.State == EntityState.Added) 
      { 
       entry.Property("IsRead").CurrentValue = false; 
      } 
     } 

     foreach (var entry in ChangeTracker.Entries().Where(entry => entry.Entity.GetType().GetProperty("Status") != null)) 
     { 
      if (entry.State == EntityState.Added) 
      { 
       entry.Property("Status").CurrentValue = "New"; 
      } 
     } 
     return base.SaveChanges(); 
    } 
} 
+0

[接続文字列をコードファーストDbContextに渡す](https://stackoverflow.com/q/4805094/102937) –

+0

アプリケーションの種類は?接続文字列は、実行可能プロジェクトの設定ファイルから取得されます。したがって、Webアプリケーションの場合、接続文字列はapp.configではなくweb.configから取得されます。 –

+0

@RobertHarvey、そうではありません。私はちょうど接続文字列を見つけることができません。それはどこかにあるはずです。 :-) –

答えて

2

私は避けるように制御を持っていることを好むだろうが、Entity Frameworkのは、接続文字列なしで動作しますですこのような状況はweb.config transformsとなります。

SQL Expressがローカルにインストールされている場合は、コンテキストコンストラクタに何も指定しないと、SQL Expressデータベース(。\ SqlExpressインスタンス)が取得されます。それ以外の場合は、ユーザープロファイルフォルダまたは\ App_DataフォルダのlocalDbを取得します。データベースは私が信じる文脈にちなんで名付けられます

コンストラクタで名前を指定すると、その名前で接続文字列が検索されますが、見つからない場合は上記の規則に従いますが、データベースの名前としてコンストラクタ値が割り当てられます。これはあなたの現在の状況です。

短いので(遅すぎる)、問題を解決するためにコンテキスト・コンストラクターを接続文字列の名前に変更してください。

public CareerTrackContext() 
    : base("freeaspconnection") 
{ 

} 

EFの接続方法については、hereを参照してください。

+0

男、あなたは私を救った。あまりにも遅くはなかった、私はちょうどコンストラクタと設定ファイルを更新しました。どうもありがとうございます! –

関連する問題