Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Populate DB Table From Model

Posted on 2013-05-23
7
322 Views
Last Modified: 2013-05-23
Hi Experts,

Thanks for reading this.

Not sure how to do populate a DB table from a Model.

So, this code takes two Models and join it to form Model3:
private static List<Model3> CombineProductAndPacakge(List<Model1> MdlProd, List<Model2> MdlPack)
{
    List<Model3> MdlCombine = new List<Model3>();
    MdlCombine = (from pd in MdlProd
                    join pk in MdlPack
                    on pd.PRODUCT equals pk.PRODUCT
                select new Model3()
                {
                    PRODUCT = pd.PRODUCT,
                    PACKAGECODE = pk.PACKAGECODE,
                    STARTDATE = pd.STARTDATE,
                    ENDDATE = pd.ENDDATE
                }).ToList();

    return (MdlCombine);
}

Open in new window

Here's where I'm not sure if I lay it out correctly for the Models:
public class Model3
{
    public string PRODUCT { get; set; }
    public string PACKAGECODE { get; set; }
    public string STARTDATE { get; set; }
    public string ENDDATE { get; set; }
}

public class MdlToDB
{
    public string PRODUCT { get; set; }
    public string PACKAGECODE { get; set; }
    public string STARTDATE { get; set; }
    public string ENDDATE { get; set; }
}

Open in new window

Here's the DBContext:
public class SomeCustomDB : DbContext
{
    public DbSet<MdlToDB> MdlToDBs { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<MdlToDB>()
    .ToTable("Some_tbl");
        base.OnModelCreating(modelBuilder);
    }
}

Open in new window

So, do we really need Model3 or could we just use MdlToDB and how do you populate Some_tbl from the contents in the Model?

TIA!
0
Comment
Question by:allanau20
  • 4
  • 3
7 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39191396
Hi allanau20;

What is the scheme of the table Some_tbl?

Also if the shape of the classes Model3 and MdlToDB are the same as shown above in your post then you can use one of them and remove the other.
0
 
LVL 5

Author Comment

by:allanau20
ID: 39191984
Good to hear from you FernandoSoto.

The scheme of table Some_tbl (a simple look up table) is:

CREATE TABLE [dbo].[Some_tbl](
	[product] [varchar](20) NOT NULL,
	[packagecode] [varchar](20) NOT NULL,
	[StartDate] [datetime] NOT NULL,
	[EndDate] [datetime] NOT NULL
) ON [PRIMARY]

Open in new window

Yes, the shape are the same, so we'll use MdlToDB.

Thanks again FernandoSoto!
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 39192129
Hi allanau20;

Because the items to update the db table Some_tbl differ only by the data type for StartDate and EndDate you will not be able to update the table directly from the values from MdlToDB.

So starting from the return from CombineProductAndPacakge with the List<MdlToDB> you can do the following.

// Where resultMdlToDB is the list returned from the call to CombineProductAndPacakge
foreach( MdlToDB m in resultMdlToDB )
{
	Some_tbl st = new Some_tbl();
	st.product = m.PRODUCT;
	st.packagecode = m.PACKAGECODE;
	// The next two variables may not be exactly correct because I do not know the formatting 
	// of STARTDATE and ENDDATE strings so you will need to modify it to a Short Date string.
	// assuming mm/dd/yyyy format.
	st.StartDate = DateTime.Parse(m.STARTDATE);
	st.EndDate = DateTime.Parse(m.ENDDATE);
	SomeCustomDBInstance.Some_tbl.Add(st);
}

SomeCustomDBInstance.SaveChanges();

Open in new window

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 5

Author Comment

by:allanau20
ID: 39192144
thank you; trying it now...
0
 
LVL 5

Author Comment

by:allanau20
ID: 39192417
Thanks FernandoSoto; that did the trick.

it wrote to the table, but threw an error with key violation; I'll figure it out ...

Thanks again! Very appreciate it!!
0
 
LVL 5

Author Closing Comment

by:allanau20
ID: 39192421
super!
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39192434
Not a problem, if you are still having issues with the key let me know.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question