Solved

Populate DB Table From Model

Posted on 2013-05-23
7
326 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 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

739 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