我正在整理一个相当简单的Code-First MVC5库存跟踪应用程序。我已将我的应用程序发送到Seed()和所有维护表(位置,供应商,状态等),我可以查看/创建/编辑/删除。我现在正在处理我的主[INV_Assets]模型的View
,但是当我尝试运行我收到的应用程序时:
System.Data.DataException :An exception occurred while initializing the database. See the InnerException for details.
InnerException :The underlying provider failed on Open.
我的HomeController
:
TrackerContext _db = new TrackerContext();
public ActionResult Index(INV_Assets defModel)
{
var assetList = _db.INV_Assets.ToList(); // EXCEPTION!
return View(assetList);
}
我试图将我的主模型[INV_Assets]传递给视图,以便我可以foreach
遍历模型类中的所有实例并将它们显示在表中。谁能看到我可能做错了什么?
下面我还包括我的模型本身[INV_Assets
]和View
:
INV_Assets :
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using GridMvc.DataAnnotations;
using System.Web.Mvc;
using Tracker.Models;
namespace Tracker.Models
{
[GridTable(PagingEnabled = true, PageSize = 30)]
public class INV_Assets
{
// Setting GridColumn Annotations allows you to use AutoGenerateColumns on view to auto create the Grid based on the model.
public int Id { get; set; }
public int Model_Id { get; set; }
[ForeignKey("Model_Id")]
public virtual INV_Models Model { get; set; }
[Required]
public int Manufacturer_Id { get; set; }
[ForeignKey("Manufacturer_Id")]
public virtual INV_Manufacturers Manufacturer { get; set; }
[Required]
public int Type_Id { get; set; }
[ForeignKey("Type_Id")]
public virtual INV_Types Type { get; set; }
[Required]
public int Location_Id { get; set; }
[ForeignKey("Location_Id")]
public virtual INV_Locations Location { get; set; }
public int Vendor_Id { get; set; }
[ForeignKey("Vendor_Id")]
public virtual INV_Vendors Vendor { get; set; }
[Required]
public int Status_Id { get; set; }
[ForeignKey("Status_Id")]
public virtual INV_Statuses Status { get; set; }
public string ip_address { get; set; }
public string mac_address { get; set; }
public string note { get; set; }
public string owner { get; set; }
public decimal cost { get; set; }
public string po_number { get; set; }
public string description { get; set; }
public int invoice_number{ get; set; }
[Required]
public string serial_number { get; set; }
[Required]
public string asset_tag_number { get; set; }
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime? acquired_date { get; set; }
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime? disposed_date { get; set; }
[Required]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime created_date { get; set; }
[Required]
public string created_by { get; set; }
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime modified_date { get; set; }
public string modified_by { get; set; }
// Flag to specify if item is available? (Not signed out, not auctioned, recycled, etc.)
//public bool available { get; set; }
}
}
[INV_Assets]查看:
@using GridMvc.Html
@using System.Collections.Generic
@model List<Tracker.Models.INV_Assets>
@{
ViewBag.Title = "Home Page";
}
<table style="width:100%;">
@foreach (var item in Model) {
<tr>
<td>@Html.DisplayFor(modelItem => item.Status)</td>
<td>@Html.DisplayFor(modelItem => item.Location)</td>
<td>@Html.DisplayFor(modelItem => item.owner)</td>
<td>@Html.DisplayFor(modelItem => item.Type)</td>
<td>@Html.DisplayFor(modelItem => item.Manufacturer)</td>
<td>@Html.DisplayFor(modelItem => item.Model)</td>
<td>@Html.DisplayFor(modelItem => item.Vendor)</td>
<td>@Html.DisplayFor(modelItem => item.description)</td>
<td>@Html.DisplayFor(modelItem => item.asset_tag_number)</td>
<td>@Html.DisplayFor(modelItem => item.serial_number)</td>
<td>@Html.DisplayFor(modelItem => item.ip_address)</td>
<td>@Html.DisplayFor(modelItem => item.mac_address)</td>
<td>@Html.DisplayFor(modelItem => item.po_number)</td>
<td>@Html.DisplayFor(modelItem => item.invoice_number)</td>
<td>@Html.DisplayFor(modelItem => item.cost)</td>
<td>@Html.DisplayFor(modelItem => item.note)</td>
<td>@Html.DisplayFor(modelItem => item.acquired_date)</td>
<td>@Html.DisplayFor(modelItem => item.disposed_date)</td>
<td>@Html.DisplayFor(modelItem => item.created_date)</td>
<td>@Html.DisplayFor(modelItem => item.created_by)</td>
<td>@Html.DisplayFor(modelItem => item.modified_date)</td>
<td>@Html.DisplayFor(modelItem => item.modified_by)</td>
</tr>
}
</table>
修改:
我有ELMAH错误日志记录正在运行,在异常发生后我点击继续后,日志显示如下:
System.Data.SqlClient.SqlException -
Cannot attach the file 'C:\Analytic-Projects\Tracker\Tracker-DEV\Tracker\Tracker\App_Data\TrackerDBDev.mdf' as database 'TrackerDBDev'.
...
答案 0 :(得分:1)
您没有发布的一件事是唯一需要的东西:Web.config中的连接字符串。由于您的数据库在错误中有.mdf
扩展名,我猜您已经部署了项目,现在无法连接到数据库。像这样的数据库文件由LocalDB使用,只能在Visual Studio的范围内使用。部署到生产服务器后,您的项目必须使用成熟的数据库,如SQL Server。您只需要在Web.config中更改项目的连接字符串(或者更好,比如Web.Release.config,因此可以在发布过程中转换Web.config),以便它指向包含完整的SQL Server实例你的应用程序的数据库。