Inserting into two tables using a view model in asp.net core/ issue with database connection

asp.net-core asp.net-mvc c# entity-framework-core

Question

What is the best way to insert into two tables at once? I am having an issue with the connection string. Below are my data models as well as my view model. I am using the code first approach with asp.net core.

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

        };

        var package = new Packaging()
        {
            CoilWeight = viewModel.Weight,
            CoilIDMIN = viewModel.MIN,
            CoilIDMAX = viewModel.MAX,
            CoilHeight = viewModel.Height,
            BuildID = viewModel.BuildID
        };
        //Models.TestNetCoreEFContext context = new Models.TestNetCoreEFContext();

        //var StudentList = context.Student.ToList();
        using ( var context = new DataContext())
        {
                context.Build.Add(build);
                package.BuildID = build.BuildID;
                context.Packaging.Add(package);
                context.SaveChanges();

            };

        return View();
    }
}

below I have the view model and models

           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; }
    [Required(AllowEmptyStrings = true)]
    public string Operator { get; set; }
    public DateTime? Date { get; set; }

    public int? Weight { get; set; }
    public int? Amount { get; set; }



    //public int? PackageID { get; set; }

    public Other Other { get; set; }
    public Packaging Packaging { get; set; }
}

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

    public int? MIN { get; set; }
    public int? MAX { get; set; }
    public int? Height { get; set; }

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

public class BuildPackagingViewModel
{
    public int BuildID { get; set; }
    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; }
    [Required(AllowEmptyStrings = true)]
    public string Operator { get; set; }
    public DateTime? Date { get; set; }

    public int? Weight { get; set; }
    public int? Amount { get; set; }
    //Packaging model
    //public int PackageID { get; set; }
    public int Weight { get; set; }

    public int? MIN { get; set; }
    public int? MAX { get; set; }
    public int? Height { get; set; }

}   

I believe the issue is in the my data context

  public class DataContext : DbContext
{
    public DataContext(DbContextOptions<DataContext> options)
    : base(options)
    { }
    public static string ConnectionString { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
            optionsBuilder.UseSqlServer(ConnectionString);
        }
    }

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

    //}
    public DbSet<Packaging> Packaging { get; set; }
    public DbSet<Build> Build { get; set; }
    public DbSet<Draw> Draw { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {

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

Here is the startup.cs file

   public void ConfigureServices(IServiceCollection services)
    {
        //foreach (var service in services)
        //{
        //    string typeName = service.ImplementationType == null ?
        //        (service.ImplementationInstance == null ? service.ImplementationFactory.GetType().ToString() : service.ImplementationInstance.ToString())
        //        : service.ImplementationType.ToString();
        //    Debug.WriteLine($"{service.ServiceType}: {typeName}, {service.Lifetime}");
        //}
        services.AddMvc();
        //services.AddDbContext<DataContext>(options =>
        //      options.UseSqlServer(Configuration.GetConnectionString("DataContext")));
        DataContext.ConnectionString = Configuration.GetConnectionString("DataContext");


    }

below is the view I was working on

 @model DemoOnMVVM.Models.ViewModels.BuildPackagingViewModel

      @{
          ViewBag.Title = "Build";
     }

      <h2>Login</h2>

       @using (Html.BeginForm())
   {
     @Html.ValidationSummary(true)

<fieldset>
    <legend > LoginViewModel </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.MIN)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.MIN)
        @Html.ValidationMessageFor(model => model.MIN)
    </div>


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

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

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

}

1
0
9/7/2017 10:04:56 PM

Popular Answer

What is actually happening - are you getting a build record but no package record?

Looking at your code I am assuming that BuildId is an IDENTITY column or similar - if you commit your Build changes first can you then access the BuildId to use when saving the package?

0
9/7/2017 3:57:51 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow