Home Best way to insert data into multiple tables ASP.NET CORE?
Reply: 0

Best way to insert data into multiple tables ASP.NET CORE?

Alex Zimmerman
1#
Alex Zimmerman Published in 2017-09-08 16:25:26Z

I believe I have the correct approach but, I am unable to get it working. I am using the code first approach. I have three tables. Next, I made a view model for Build-Inspection. The inspection and packaging tables are both optional tables to be inserted into based upon if the category is packaging or inspection. My view will not display. Why would this be? How come I have no errors?

 public class Build
{
    public int BuildID { get; set; }
    // order of each individual step
    public int BuildStep { get; set; }

    public string Category { get; set; }

    public string SubCategory { get; set; }

    public string Details { get; set; }

    public string AdditionalNotes { get; set; }

    public DateTime? Date { get; set; }

    public int? Weight { get; set; }

    public Inspection Inspection { get; set; }
    public Packaging Packaging { get; set; }
}

Packaging Table

      public class Packaging
       {
    public int PackagingID { get; set; }
    public int MaxWeight { get; set; }

    public int? MINWeight { get; set; }
    public int? MAXWidth { get; set; }
    public int? MaxHeight { get; set; }

    public int BuildID {get; set;}
    public Build Build { get; set; }
}

Inspection table

  public class Inspection
 {
  public int InspectionID {get; set;}
  public int Type  {get; set;}
  public int TensileMAX {get; set;}

  public int BuildID {get; set;}
  public Build Build { get; set; }
 }

View model for build-inspection

   public class BuildInspectionViewModel
  {
public int BuildStep { get; set; }

public string Category { get; set; }

public string SubCategory { get; set; }

public string Details { get; set; }

public string AdditionalNotes { get; set; }

public DateTime? Date { get; set; }

public int? Weight { get; set; }

public int Type  {get; set;}

public int TensileMAX {get; set;}

}

Next I made a controller that should be able to insert into both tables using the view

    //grabbing my db context or my database
    private readonly DataContext _context;

    public BuildController(DataContext context)
    {
        _context = context;
    }

   [HttpPost]
    public ActionResult Create(BuildPackagingViewModel viewModel)
    {
        Build build = new Build()
        {
            BuildStep = viewModel.BuildStep,
            Category = viewModel.Category,
            SubCategory = viewModel.SubCategory,
            Details = viewModel.Details,
            AdditionalNotes = viewModel.AdditionalNotes

        };

        Inspection inspection = new Inspection()
        {
            Type = viewModel.Type,
            TensileMax = viewModel.TensileMax,

        };

              _context.Build.Add(build);
               inspection.BuildID = build.BuildID;
              _context.Inspection.Add(inspection);
              _context.SaveChanges();



        return View();

Then I made a view so I could insert into the inspection and build table.

     @model DemoOnMVVM.Models.ViewModels.BuildInspectionViewModel

 @{
      ViewBag.Title = "Index";
  }

  <h2>Index</h2>
    <form asp-controller="BuildInspection" asp-action="Create" method="post" 
    class="form-horizontal" role="form">

<fieldset>
    <legend> BuildInspectionViewModel </legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.BuildStep)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.BuildStep)
        @Html.ValidationMessageFor(model => model.BuildStep)
    </div>

       <div class="editor-label">
        @Html.LabelFor(model => model.Category)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Category)
        @Html.ValidationMessageFor(model => model.Category)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.SubCategory)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.SubCategory)
        @Html.ValidationMessageFor(model => model.SubCategory)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Details)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Details)
        @Html.ValidationMessageFor(model => model.Details)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.AdditionalNotes)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.AdditionalNotes)
        @Html.ValidationMessageFor(model => model.AdditionalNotes)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Weight)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Weight)
        @Html.ValidationMessageFor(model => model.Weight)
    </div>

        <div class="editor-label">
        @Html.LabelFor(model => model.Type)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Type)
        @Html.ValidationMessageFor(model => model.Type)
    </div>

        <div class="editor-label">
        @Html.LabelFor(model => model.TensileMax)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.TensileMax)
        @Html.ValidationMessageFor(model => model.TensileMax)
    </div>
<p>
        <input type="submit" value="Create" />
    </p>
</fieldset>

Here is my DataContext that I used to scaffold my database

    public class DataContext : DbContext
{

  public DataContext(DbContextOptions<DataContext> options) : base(options)
       {

    }
    public DbSet<Packaging> Packaging { get; set; }
    public DbSet<Build> Build { get; set; }
    public DbSet<Inspection> Inspection { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
    {

        modelBuilder.Entity<Inspection>().ToTable("Inspection");
        modelBuilder.Entity<Build>().ToTable("Build");
        modelBuilder.Entity<Packaging>().ToTable("Packaging");
    }
}

Here is my startup.cs file

public void ConfigureServices(IServiceCollection services)
    {

        services.AddMvc();
        services.AddDbContext<DataContext>(options =>

options.UseSqlServer(Configuration.GetConnectionString("DataContext")));

The appsettings.json file

  {
   "ConnectionStrings": {
    "DataContext": "Mysqlserverconnection"
  },

Overall, I am not sure why my view will not display. I only scaffolded the packaging, build and inspection tables using the code first approach. Below are my resource links.

(Best way to insert data to multiple table MVC ASP)

(https://docs.microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext)

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.301792 second(s) , Gzip On .

© 2016 Powered by cudou.com design MATCHINFO