使用实体框架与SQL Server 2012(本地* .mdf数据库) [英] Using Entity framework with SQL Server 2012 (local *.mdf database)
问题描述
DbContext
数据库。我无法连接到数据库。我怀疑问题是在我的连接字符串,但我不知道。我想要的数据库将由两个表组成,其中定义如下: public class EmployeeRequest
{
[Key]
public int EmployeeRequestId {get;组; }
public string FirstName {get;组; }
public string LastName {get;组; }
public string UserName {get;组; }
public int ExtNumber {get;组; }
public bool isProcessed {get;组; }
public virtual ICollection< ChangeOrder> ChangeOrders {get;组;
}
和
public class ChangeOrder
{
[Key]
public int ChangeOrderId {get;组; }
public short Operation {get;组; } // 0 - Add,1- delete
public int TargetExt {get;组; }
public short Status {get;组; } // 0-待处理,1-批准,2-被拒绝
public DateTime DtRequested {get;组; }
public DateTime DtProcessed {get;组; }
public DateTime DtChangesApplied {get;组; }
public int EmployeeRequestID {get;组; }
public virtual EmployeeRequest Request {get;组;
}
DbContext
类是:
public class RequestsContext:DbContext
{
public DbSet< EmployeeRequest> EmployeeRequests {get;组; }
public DbSet< ChangeOrder> ChangeOrders {get;组;
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove< PluralizingTableNameConvention>();
}
}
连接字符串:
< add name =RequestsContext
providerName =System.Data.SqlClient
connectionString =Data Source =(LocalDb) ;数据库= Requests.mdf; Trusted_Connection =真/>
我得到的错误表示无法连接到主机。
我也不知道为什么会发生这种情况。有任何想法吗?
请检查应用程序是否可以访问.mdf文件位置。如果您的应用程序驻留在myDocuments等类型的文件夹中,则默认情况下应用程序可能无法访问它。
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.
这篇关于使用实体框架与SQL Server 2012(本地* .mdf数据库)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!