我不知道我做错了什么,非常努力地设置数据库项目,并且不断收到与connectionstring dbcontext相关的错误。
我有一个applicationdbcontext,它只是连接到localdb
问题是我的其他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;");
}
在startup.cs我有这个代码.. services.AddDbContext(options =&gt; options.UseSqlServer(Configuration.GetConnectionString( “DefaultConnection”))); services.AddDbContext(options =&gt; options.UseSqlServer(Configuration.GetConnectionString( “ProdConnection”)));
在我的appsettings配置中我有这个代码..
"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 :(得分:0)
您应该删除optionsBuilder.UseSqlServer("...
方法中的OnConfiguring
行。
然后像这样在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个单独的地方定义了连接,我想这引发了异常。