我是编程的初学者,现在我正尝试在ASP.Net中构建Web应用程序。我正确的问题是如何首先使用代码创建DBContext。 这是我的模特:
public class Messages {
public int MessageID { get; set; }
[Required]
[StringLength(300, MinimumLength = 3)]
public string MessageContent { get; set; }
public virtual ApplicationUser Sender { get; set; }
public virtual ApplicationUser Receiver { get; set; }
}
public class FriendRequest {
public int RequestId { get; set; }
public virtual ApplicationUser RequestFrom { get; set; }
public virtual ApplicationUser RequestTo { get; set; }
public bool Confirmed { get; set; }
}
public class DejtingsidaDbContext : DbContext {
public DejtingsidaDbContext() : base() { }
public virtual DbSet<Messages> Messages { get; set; }
public virtual DbSet<FriendRequest> Requests { get; set; }
}
这是HomeController:
public class HomeController : Controller {
DejtingsidaDbContext db = new DejtingsidaDbContext();
public ActionResult Index() {
return View();
}
public ActionResult About() {
ViewBag.Message = "Your application description page.";
return View();
}
public ActionResult Contact() {
ViewBag.Message = "Your contact page.";
return View();
}
}
我试图在Web.config中创建一个连接字符串,但是当我第一次运行该程序时,然后当我检查是否已创建数据库时,尚未创建它。请帮助新手!
答案 0 :(得分:0)
在web.cofig文件中传递连接字符串,如下所示。
<connectionStrings>
<add name="yourconnectinstringName"
connectionString="
Data Source= DatabaseServerName(example:uzainPC);
Integrated Security=true;
Initial Catalog=YourDatabaseName(example:SchoolSystem);
uid=YourUserName;
Password=yourpassword; "
providerName="System.Data.SqlClient"
/>
</connectionStrings>
而不是从下面显示的这些行中删除虚拟的。
public class DejtingsidaDbContext : DbContext {
public DejtingsidaDbContext() : base() { }
public DbSet<Messages> Messages { get; set; }
public DbSet<FriendRequest> Requests { get; set; }
}