Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Populate DB Table From Model

Posted on 2013-05-23
7
Medium Priority
?
332 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 64

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 64

Accepted Solution

by:
Fernando Soto earned 2000 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
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
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 64

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

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

927 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