I am trying to create a database in an MVC project. Code-First approach. I wrote my models and a DbContext
database. I am having trouble making the connection to the database. My suspect that the problem is in my connection string but I am not sure. the database I want will consist of two tables, which are defined as follows:
public class EmployeeRequest
{
[Key]
public int EmployeeRequestId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string UserName { get; set; }
public int ExtNumber { get; set; }
public bool isProcessed { get; set; }
public virtual ICollection<ChangeOrder> ChangeOrders { get; set; }
}
and
public class ChangeOrder
{
[Key]
public int ChangeOrderId { get; set; }
public short Operation { get; set; } //0 - Add, 1- delete
public int TargetExt { get; set; }
public short Status { get; set; } //0- Pending, 1- Approved, 2- Denied
public DateTime DtRequested { get; set; }
public DateTime DtProcessed { get; set; }
public DateTime DtChangesApplied { get; set; }
public int EmployeeRequestID { get; set; }
public virtual EmployeeRequest Request { get; set; }
}
The DbContext
class is:
public class RequestsContext : DbContext
{
public DbSet<EmployeeRequest> EmployeeRequests { get; set; }
public DbSet<ChangeOrder> ChangeOrders { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
Connection string:
<add name="RequestsContext"
providerName="System.Data.SqlClient"
connectionString="Data Source=(LocalDb);Database=Requests.mdf;Trusted_Connection=True"/>
The error I get says that the connection could not be made to the host.
I am nor sure why this is happening. Any Ideas?
Please check the application has access to .mdf file location. If your application resides in "myDocuments" etc type of folders, the application may not have access to it by default.