同时创建包含相关数据的实体

时间:2018-03-21 08:20:21

标签: asp.net sql-server entity-framework

我正在尝试使用集成的Entity Framework开发一个html页面。 这是我的关系(为简单起见,我删除了一些不相关的属性): enter image description here

...这样 用户可以创建一个或多个工具,工具可以有多个参数。 我的解决方案中有三个类: 用户:

    public class User
   {
    public string Id { get; set; }
    public string Name { get; set; }
    public string Surname { get; set; }
    public string PasswordHash { get; set; }
    public string Mail { get; set; }

    public ICollection<Tool> Tools { get; set; }
    }

工具:

    public class Tool
{
    public string UserId { get; set; }
    public string Id { get; set; }
    public string Description { get; set; }
    public float Version { get; set; }
    public DateTime LastUpdate { get; set; }

    public ICollection<Parameter> Parameters { get; set; }
}

参数:

    public enum ParameterType {
    STRING,SWITCH
}

public class Parameter
{
    public string ToolId { get; set; }
    public string Id { get; set; }
    public string Description { get; set; }
    public bool Mandatory { get; set; }
    public string DefaultValue { get; set; }
    public ParameterType Type{ get; set; }
}

我能够在单个视图中加载相关数据,但我无法同时创建相关数据。 我正在尝试制作一个html页面,用它的相关参数创建新工具:

@page
@model POICTWebConsole.Pages.Tools.CreateModel

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

<h2>Create Tool</h2>
<div class="row">
    <hr />
    <form method="post">
        <div class="col-md-6">
            <h4>General Info</h4>
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        <div class="form-group">
            <label class="control-label">Author</label>
            <input class="form-control" type="text" asp-for="@Model.Username" readonly>
        </div>
        <div class="form-group">
            <label asp-for="Tool.Id" class="control-label">Tool Name</label>
            <input asp-for="Tool.Id" class="form-control" />
            <span asp-validation-for="Tool.Id" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="Tool.Description" class="control-label"></label>

            <textarea asp-for="Tool.Description" rows="4" cols="50" class="form-control">
            </textarea>
            <span asp-validation-for="Tool.Description" class="text-danger"></span>
        </div>
    </div>
    <div class="col-md-6">
        <h4>Create Parameter</h4>
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        <div class="form-group">
            <label class="control-label" asp-for="Parameter.Id">Parameter Name</label>
            <input class="form-control" type="text" asp-for="@Model.Parameter">
        </div>
        <div class="form-group">
            <label asp-for="Tool.Description" class="control-label"></label>

            <textarea asp-for="Tool.Description" rows="4" cols="50" class="form-control">
            </textarea>
            <span asp-validation-for="Tool.Description" class="text-danger"></span>
        </div>

        <div class="form-group">
            <input type="submit" value="Add" class="btn btn-default" />
        </div>
    </div>
    <div>
        <h4>@Html.DisplayNameFor(model => model.Pippo)</h4>
        <table class="table">
            <tr>
                <th>Name</th>
                <th>Description</th>
            </tr>
            @foreach (var item in Model.Pippo)
            {
                <tr>
                    <td>
                        @item.Id
                    </td>
                    <td>
                        @item.Description
                    </td>
                </tr>
    }
        </table>
    </div>
    <div class="form-group">
        <input type="submit" value="Create" class="btn btn-default" />
    </div>
</form>
</div>

<div>
    <a asp-page="Index">Back to List</a>
</div>

@section Scripts {
   @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

这是PageModel:

public class CreateModel : PageModel
{
    private readonly POICTWebConsole.Data.POICTDBContext _context;
    private List<Parameter> newParameters;

    [BindProperty]
    public string Username { get; set; }

    [BindProperty]
    public Parameter Parameter { get; set; }

    [BindProperty]
    public List<Parameter> Pippo { get; set; }

    [BindProperty]
    public Tool Tool { get; set; }

    public CreateModel(POICTWebConsole.Data.POICTDBContext context)
    {
        _context = context;
    }

    public IActionResult OnGet()
    {
        Username = Environment.UserName.Replace("SPIMI\\", "");
        Pippo = new List<Parameter>();
        for (int i = 0; i < 3; i++)
        {
            Pippo.Add(new Parameter() { ToolId = "Get-Pc", Id = "Parameter " + i, Description="prova", Mandatory = false, Type = ParameterType.STRING, DefaultValue=null }); 
        }
        newParameters = Pippo;
        return Page();
    }

    public async Task<IActionResult> OnPostAsync()
    {
        if (!ModelState.IsValid)
        {
            return Page();
        }

        Tool newTool = new Tool() { UserId = Username, LastUpdate=DateTime.Now, Version=1.0F, Parameters=newParameters };
        bool validTool = await TryUpdateModelAsync<Tool>(newTool,"Tool", t => t.Id, t => t.Description);

        if (validTool)
        {
            _context.Tools.Add(newTool);
            await _context.SaveChangesAsync();

            foreach (Parameter item in newTool.Parameters)
            {
                _context.Parameters.Add(item);
            }
            await _context.SaveChangesAsync();

            return RedirectToPage("./Index");
        }
        return null;
    }
}

我有一个参数表单来创建一个新参数,当我点击添加按钮时,我会在下表中看到新参数。 例如,我用简单数据填充了我的参数列表,但是当我创建新工具时,我也无法创建它的参数。 我怎么能这样做?如何实时查看下表中添加的参数?

P.S。:这是我的DBContext:

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

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Parameter>().HasKey(pk => new { pk.ToolId, pk.Id});
    }

    public DbSet<User> Users { get; set; }
    public DbSet<Tool> Tools { get; set; }
    public DbSet<Parameter> Parameters { get; set; }
}

0 个答案:

没有答案