Solved

Populate DB Table From Model

Posted on 2013-05-23
7
312 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 62

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 62

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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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 62

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

760 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now