Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > BOOK: Beginning ASP.NET 4 : in C# and VB
| Search | Today's Posts | Mark Forums Read
BOOK: Beginning ASP.NET 4 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old January 26th, 2013, 10:28 PM
Authorized User
Points: 135, Level: 2
Points: 135, Level: 2 Points: 135, Level: 2 Points: 135, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2012
Posts: 27
Thanks: 4
Thanked 2 Times in 2 Posts
Default Creating programmatically a new table using entities

I wanted to create programmatically a new table, which will list the user names and emails of all the users. Here's the code:

Code:
var query =
           from u in myEntities.aspnet_Users
           join m in myEntities.aspnet_Membership
           on u.UserId
           equals m.UserId into userList
           select new
           {
             UserName = u.UserName,
             Email = u.aspnet_Membership.Email,
           };
      myEntities.SaveChanges();
I get no error when run the code, however, the userList table is not created.
Any help on this issue will be highly appreciated.
By the way, creating tables programmatically is not covered in this book. Is it covered in "Professional ASP.NET 4 in C# and VB"?
  #2 (permalink)  
Old January 27th, 2013, 04:51 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

This example does *not* create new tables; all it does is select new data, store it in a new, anonymous object that can then be used to display in the frontend. It's the equivalent of a JOIN in SQL that selects data from multiple tables but doesn't affect the database's structure.

You can't use EF to create new tables; based on the database schema some code needs to run first that generates .NET objects based on the underlying tables. You can create tables using Management Studio, DDL (e.g. CREATE TABLE statements) or programmatically using code. For that last option, take a look here: http://www.christophdebaene.com/blog...g-net-and-smo/ Just realize that this just creates a table; it doesn't make it available in your EF diagram; you have the run the Update wizard for that.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
  #3 (permalink)  
Old January 27th, 2013, 08:02 AM
Authorized User
Points: 135, Level: 2
Points: 135, Level: 2 Points: 135, Level: 2 Points: 135, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2012
Posts: 27
Thanks: 4
Thanked 2 Times in 2 Posts
Default Thank you

The confirmation that "You can't use EF to create new tables" is very important.
I thought that the EF implemented all the basic features of SQL and tried to stay within EF with my entire code, but now I see that I have to mix in some SOL too.
Thank you very much.
  #4 (permalink)  
Old January 27th, 2013, 08:13 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Not the Model First and Database First approaches of EF. EF also supports Code First that does create the database based on classes you create in your project (as opposed to the query you tried to use). However, that still requires you to write classes up front that are then turned into tables the first time you load an application with the changes. This is quite a different model, and requires quite a bit of knowledge on EF, Pocos and object modeling. For more info: http://msdn.microsoft.com/en-us/data/ee712907

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
  #5 (permalink)  
Old January 27th, 2013, 12:19 PM
Authorized User
Points: 135, Level: 2
Points: 135, Level: 2 Points: 135, Level: 2 Points: 135, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2012
Posts: 27
Thanks: 4
Thanked 2 Times in 2 Posts
Default Thank you

Thanks a lot for this information.


Similar Threads
Thread Thread Starter Forum Replies Last Post
creating code programmatically mumphis Excel VBA 0 September 2nd, 2005 09:29 AM
Report - programmatically creating penta Access 12 May 19th, 2005 04:53 PM
Creating a table programmatically Bob Burns ASP.NET 1.0 and 1.1 Basics 2 July 15th, 2004 11:44 AM
Creating report programmatically liliarum BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 1 July 6th, 2004 09:34 AM
Creating Tables Programmatically stu9820 ASP.NET 1.0 and 1.1 Basics 10 October 22nd, 2003 04:25 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.