Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Populate DB Table From Model

Posted on 2013-05-23
7
Medium Priority
?
329 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
[X]
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
  • 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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

705 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