如何在details.cshtml页面

时间:2019-01-24 17:09:15

标签: asp.net asp.net-mvc crud

我在同一SQL服务器上有两个数据库。数据库1是“规格”数据库,数据库2是“零件目录”数据库。零件目录仅是R&D组的参考数据。所有创建和更新将在Specs数据库中。详细信息页面仅适用于Specs数据库。当我尝试在第二个数据库中添加错误消息时。对于我的示例:我试图从零件目录中提取零件描述。

Startup.cs

var connection = "Data Source = NAVSQL2017\\NAVSQL2017; Initial Catalog = SPECS; Persist Security Info = True; User ID = test; Password = xxx";
            services.AddDbContext<RovingContext>(options => options.UseSqlServer(connection));


var connection1 = "Data Source = NAVSQL2017\\NAVSQL2017; Initial Catalog = PARTS_CATALOG; Persist Security Info = True; User ID = test; Password = xxx";
            services.AddDbContext<ItemContext>(options => options.UseSqlServer(connection1));

appsettings.json

"ConnectionStrings": {
    "RovingContext": "Server=(localdb)\\mssqllocaldb;Database=RovingContext-0afacde4-1953-4537-8410-5d670f37debc;Trusted_Connection=True;MultipleActiveResultSets=true",
    "ItemContext": "Server=(localdb)\\mssqllocaldb;Database=ItemContext-0afacde4-1953-4537-8410-5d670f37debc;Trusted_Connection=True;MultipleActiveResultSets=true"
  }

Context.cs

namespace Spec4.Models
{
    public class RovingContext : DbContext
    {
        public RovingContext (DbContextOptions<RovingContext> options)
            : base(options)
        {
        }

        public DbSet<Spec4.Models.RovingHeader> Roving_Header { get; set; }
        public DbSet<Spec4.Models.RovingLine> Roving_Line { get; set; }

    }

     public class ItemContext : DbContext
    {
        public ItemContext (DbContextOptions<ItemContext> options)
            : base(options)
        {
        }


        public DbSet<Spec4.Models.Item> Item { get; set; }

    }
}

型号:

namespace Spec4.Models
{
    public class RovingHeader
    {
        [Key]
        [Required]
        public string Roving_No { get; set; }
        [Required]
        public string Description { get; set; }
        [DataType(DataType.Date)]
        public DateTime Mod_Date { get; set; }

        public ICollection<RovingLine> RovingLine { get; set; }

    }
}

namespace Spec4.Models
{
    public class RovingLine
    {

        public string Roving_No { get; set;}
        [Required]
        public string Item_No { get; set; }
        [Required]
        public decimal QTY { get; set; }
        [Key]
        public int RowId { get; set; }


        [ForeignKey("No_")]
        public Item Item { get; set; }

        [ForeignKey("Roving_No")]
        public RovingHeader RovingHeader { get; set; }


    }
}

namespace Spec4.Models
{
    public class Item
    {
        [Column("Item")]
        [Key]
        public string No_ { get; set; }
        public string Description { get; set; }

        public ICollection<RovingLine> RovingLine { get; set; }

    }
}

Details.cshtml

 @page
@model Spec4.Pages.DetailsModel

@{
    ViewData["Title"] = "Details";
}

<h1>Details</h1>

<div>
    <h4>RovingHeader</h4>
    <hr />
    <dl class="row">
        <dt class="col-sm-2">
            @Html.DisplayNameFor(model => model.RovingHeader.Description)
        </dt>
        <dd class="col-sm-10">
            @Html.DisplayFor(model => model.RovingHeader.Description)
        </dd>
        <dt class="col-sm-2">
            @Html.DisplayNameFor(model => model.RovingHeader.Mod_Date)
        </dt>
        <dd class="col-sm-10">
            @Html.DisplayFor(model => model.RovingHeader.Mod_Date)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.RovingHeader.RovingLine)
                   </dt>
        <dd>
            <table class="table">
                <tr>
                    <th>Item</th>
                    <th>QTY</th>
                </tr>
                @foreach (var item in Model.RovingHeader.RovingLine)
                {
                <tr>
                    <td>
                        @Html.DisplayFor(modelItem => item.Item_No)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Item.Description)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.QTY)
                    </td>

                </tr>
                }
            </table>
        </dd>

    </dl>
</div>
<div>
    <a asp-page="./Edit" asp-route-id="@Model.RovingHeader.Roving_No">Edit</a> |
    <a asp-page="./Index">Back to List</a>
</div>

details.cshtml.cs

namespace Spec4.Pages
{
    public class DetailsModel : PageModel
    {
        private readonly Spec4.Models.RovingContext _context;


        public DetailsModel(Spec4.Models.RovingContext context)
        {
            _context = context;

        }

        public RovingHeader RovingHeader { get; set; }


        public async Task<IActionResult> OnGetAsync(string id)
        {
            if (id == null)
            {
                return NotFound();
            }

            RovingHeader = await _context.Roving_Header

                .Include(s => s.RovingLine)

                    .ThenInclude(e => e.Item)
                .AsNoTracking()
                .FirstOrDefaultAsync(m => m.Roving_No == id);

            if (RovingHeader == null)
            {
                return NotFound();
            }
            return Page();
        }
    }
}

我得到的错误是SQL错误: 处理请求时发生未处理的异常。 SqlException:无效的对象名称“ Item”。 System.Data.SqlClient.SqlCommand + <> c.b__122_0(任务结果)

任何建议将不胜感激。

0 个答案:

没有答案