2017-11-07 47 views
-1

私は間違っていることを知らず、データベースプロジェクトをセットアップするのに非常に苦労し、connectionstring dbcontextに関連したエラーを続けます。ConnectionStrings EFコアのDbContext

私はちょうど

問題は私のデータが間違った場所にあると思われると、私はそれを修正するかどうかはわからないところで私の他のdbcontextあるlocaldbに接続している1 applicationdbcontextを持っています。このコードは、私のモデルフォルダ、私はこのコードを持っているstartup.csで

public DbSet<Customer> Customers { get; set; } 
public DbSet<Job> Jobs { get; set; } 
public DbSet<Order> Orders { get; set; } 
public DbSet<Staff> Staff { get; set; } 
public DbSet<RequestType> RequestType { get; set; } 
public DbSet<CustomerJob> CustomerJobs { get; set; } 

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) 
{ 
    optionsBuilder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=Customers;Trusted_Connection=True;"); 
} 

... services.AddDbContext(=> options.UseSqlServer(Configuration.GetConnectionString( "DefaultConnectionを")のオプション))です。 services.AddDbContext(options => options.UseSqlServer(Configuration.GetConnectionString( "ProdConnection")));まだ私はこのコードを持っている私のAppSettings configに

..

"ConnectionStrings": { 
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=BRSCRM;Trusted_Connection=True;MultipleActiveResultSets=true", 
    "ProdConnection": "Server=(localdb\\mssqllocaldb;Database=Customers;Trusted_Connection=True;MultipleActiveResults=true" 
}, 

私は設定が供給されていること、エラーが使用されていない取得私のプロジェクト、実際に制御のDI反転、その袋を実行します猫とその火の上に!

+0

エラーは次のようなものです.AddDbContextがコンフィグレーションで呼び出されましたが、コンテキストタイプ 'CustomerContext'はパラメータのないコンストラクタのみを宣言します。つまり、AddDbContextに渡された設定は決して使用されません。構成がAddDbContextに渡された場合、 'CustomerContext'はDbContextOptions を受け入れるコンストラクタを宣言し、DbContextの基本コンストラクタにそのコンストラクタを渡す必要があるため、onConfiguringをオーバーライドする必要があります。 。 – webdev8183

+0

ugh明らかに、startup.csファイル内の文字列はまったく必要なく、onconfiguringからのオーバーライドがうまくいきましたが、それはばかげて同じ接続文字列を2回使用したかったのです。 – webdev8183

答えて

0

お客様のOnConfiguringメソッドでoptionsBuilder.UseSqlServer("...行を削除する必要があります。

次に、このようにDbContextクラスにコンストラクタを追加します。

public YourDbContext(DbContextOptions<YourDbContext> options) : base(options) 
{ 
} 

追加DbContextクラス..

public class CustomerContext : DbContext 
{ 
    public DbSet<Customer> Customers { get; set; } 
    public DbSet<Job> Jobs { get; set; } 
    public DbSet<Order> Orders { get; set; } 
    public DbSet<Staff> Staff { get; set; } 
    public DbSet<RequestType> RequestType { get; set; } 
    public DbSet<CustomerJob> CustomerJobs { get; set; } 
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) 
    { 
     optionsBuilder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=Customers;Trusted_Connection=True;"); 
    } 
    protected override void OnModelCreating(ModelBuilder modelBuilder) 
    { 
     //modelBuilder.Entity<CustomerJob>() 
     // .HasKey(c => new { c.JobId, c.CustomerId }); 
     //code to require a staff member be assigned.. 

     // modelBuilder.Entity<Staff>().Property(s => s.Name).IsRequired(); 
     // modelBuilder.Entity<Customer>().Property(c => c.AssignedStaff).IsRequired(); 
    } 


} 


public class CustomerJob 
{ 
    public int CustomerJobId { get; set; } 
    public int CustomerId { get; set; } 
    public DateTime RequestDate { get; set; } 
    public int JobId { get; set; } 
    public Job Job { get; set; } 
} 

public class Job 
{ 
    public int JobId { get; set; } 
    public int CustomerId { get; set; } 

    public string BusinessName { get; set; } 
    public string Name { get; set; } 
    public string JobDescription { get; set; } 
    public string ServiceType { get; set; } 
    public string GoogleLink { get; set; } 
    public string PoisLink { get; set; } 
    public bool EquisRendered { get; set; } 
    public bool NadirsRemoved { get; set; } 
    public string FolderLink { get; set; } 
    public string ReviewPosted { get; set; } 
    public string Ingestion { get; set; } 
    public string Moderated { get; set; } 
    public bool Delivered { get; set; } 
    public string CustomerReview { get; set; } 
    public string PublishedLink { get; set; } 
    public DateTime RequestDate { get; set; } 
    public DateTime LastModifiedDate { get; set; } 
    public DateTime ScheduleShootDate { get; set; } 
    public DateTime CompletionDate { get; set; } 
    public List<CustomerJob> CustomerJobs { get; set; } 
    public Staff AssignedStaff { get; set; } 
} 

public class Staff 
{ 
    public int StaffId { get; set; } 
    public string Name { get; set; } 
    public string Phone { get; set; } 
    public string EMail { get; set; } 
} 

public class Order 
{ 
    public int OrderID { get; set; } 
    public int CustomerID { get; set; } 
    public int Order_Detail_Id { get; set; } 
    public List<Job> Job { get; set; } 
} 

public class RequestType 
{ 
    public int ID { get; set; } 
    public string Description { get; set; } 
} 

}

Startup.csクラス..とにかく

public class Startup 
{ 
    public Startup(IConfiguration configuration) 
    { 
     Configuration = configuration; 
    } 

    public IConfiguration Configuration { get; } 

    // This method gets called by the runtime. Use this method to add services to the container. 
    public void ConfigureServices(IServiceCollection services) 
    { 
     services.AddDbContext<ApplicationDbContext>(options => 
      options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); 
     services.AddDbContext<CustomerContext>(); 

     services.AddIdentity<ApplicationUser, IdentityRole>() 
      .AddEntityFrameworkStores<ApplicationDbContext>() 
      .AddDefaultTokenProviders(); 

     // Add application services. 
     services.AddTransient<IEmailSender, EmailSender>(); 
     // Add Oauth Options 
     /* Third Party Login Authenticaton Options Google */ 
     services.AddAuthentication().AddGoogle(googleOptions => 
     { 
      googleOptions.ClientId = Configuration["Authentication:Google:ClientId"]; 
      googleOptions.ClientSecret = Configuration["Authentication:Google:ClientSecret"]; 
     }); 
     /* End Google Options */ 
     /* Begin Facebook Options */ 
     services.AddAuthentication().AddFacebook(facebookOptions => 
     { 
      facebookOptions.AppId = Configuration["Authentication:Facebook:AppId"]; 
      facebookOptions.AppSecret = Configuration["Authentication:Facebook:AppSecret"]; 
     }); 

     /* End Facebook Options */ 

     /* Begin Microsoft Options */ 
     services.AddAuthentication().AddMicrosoftAccount(microsoftOptions => 
     { 
      microsoftOptions.ClientId = Configuration["Authentication:Microsoft:ApplicationId"]; 
      microsoftOptions.ClientSecret = Configuration["Authentication:Microsoft:Password"]; 
     }); 

     /* End Microsoft Options */ 

     /* Twitter Options */ 
     services.AddAuthentication().AddTwitter(twitterOptions => 
     { 
      twitterOptions.ConsumerKey = Configuration["Authentication:Twitter:ConsumerKey"]; 
      twitterOptions.ConsumerSecret = Configuration["Authentication:Twitter:ConsumerSecret"]; 
     }); 
     /* End Twitter Options */ 
     /* Begin Identity Options Configuration */ 
     services.AddMvc(); 
     services.AddAuthorization(options => 
     { 
      options.AddPolicy("RequireAdminRole", policy => policy.RequireRole("Admin")); 
     }); 
    } 

その今働いて、私のミスは私が定義されていたことでした2つの別々の場所で接続し、私は例外を投げたと思います。

+0

:base(options)何もしませんが、実装を持たない基本クラスを呼び出すだけで、接続文字列は必要なくなりました。今は2回動作していますが、入力に感謝します。 – webdev8183

+0

あなたはuse services.AddDbContext(options => options.UseSqlServer(Configuration.GetConnectionString( "DefaultConnection")));を使用します。スタートアップ.cs。この接続文字列オプションパラメータは、dbコンテキストコンストラクタで受け付ける必要があります。 –

+0

いいえ私は起動時にコンストラクタで接続を定義しました。私は引数なしで渡していました。 – webdev8183