【发布时间】:2016-08-22 11:14:34
【问题描述】:
我在 .NET Framework 4.5 下发布了一个 ASP.NET MVC 5 应用程序。 服务器是 Windows 2012 R2 专用服务器。数据库框架为MS SQL Server 2016。
我将连接字符串更改为:
<connectionStrings >
<add name="ApplicationDbContext"
connectionString="Server=server.de;Database=DataBase;User ID=UserName;Password=xxxxxxxx;Trusted_Connection=False;"
providerName="System.Data.SqlClient"/>
</connectionStrings>
当我之后构建项目时,服务器资源管理器刷新,我可以看到数据库。我现在也可以从服务器资源管理器完全访问数据库。 但不知何故,我项目中的每个查询都没有返回数据。 因此,我尝试从 Controller 方法访问数据库:
using (SqlConnection s = new SqlConnection("Data Source=server.de;Initial Catalog=DataBase;Integrated Security=False;User ID=UserName;Password= xxxxxxxx "))
{
s.Open();
SqlCommand cmd = s.CreateCommand();
cmd.CommandText = "SELECT * FROM AspNetUsers;";
string allUsers = (string)cmd.ExecuteScalar();
}
像这样访问数据库非常有效。
为什么我不能通过我的标准 DB-Context 访问数据库?它仅适用于代码中的 SqlConnection。 ConnectionString 对吗?我是不是忘了别的东西?
(我已经在 VS 中将 SSTP 更新为 2016)
这是我的数据库上下文:
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
return userIdentity;
}
public int? BauunternehmenId { get; set; }
public virtual Bauunternehmen Bauunternehmen { get; set; }
public int? BauherrId { get; set; }
public virtual Bauherr Bauherr { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public virtual DbSet<Bauunternehmen> MyBauunternehmen { get; set; }
public virtual DbSet<Bauherr> MyBauherr { get; set; }
public virtual DbSet<Baustelle> MyBaustelle { get; set; }
public virtual DbSet<Kamera> MyKamera { get; set; }
public virtual DbSet<Kameraprojekt> MyKameraprojekt { get; set; }
public virtual DbSet<Bild> MyBild { get; set; }
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
【问题讨论】:
-
你能把你的EF代码吗?
-
感谢您的回答。我添加了数据库上下文。你是这个意思吗?
-
希望你得到答案吗?如果是这样,请以赞成票接受它:)
标签: asp.net-mvc entity-framework connection-string sql-server-express