Creating complex object using POCO in EF - poco

I am trying to understand how to create a complex POCO which in the backend tied to a stored procedure or multiple tables. All the examples I have seen are simple one table to one entity or two tables. Could some one point me to a link or example on how to create a complex POCO which talks to stored procedure or multiple table. Or is it just my imagination?
Thanks,

Found the following link and it has some good information, thought share it with anyone interested.
http://blogs.msdn.com/b/adonet/archive/2009/05/21/poco-in-the-entity-framework-part-1-the-experience.aspx

Related

Creating Classes from Exising Database

I have ben following through a tutorial that shows how to get started creating a Web Forms Application in ASP.NET...and have got to the part where you create Data Classes. These assume you are starting from scratch and generating the database from code - however - I already have a database created and populated from elsewhere.
Entering line by line to create the Class for each table seems very laborious. (I'm using VB.Net and VS 2013 by the way)
Q1. Can anyone help by suggesting a more automated way of doing this? Surely there is a tool to grab the schema?
Q2. Although the tutorial mentions Entity Framework and Code Behind briefly - having done some reading and research - I am very confused by all of the different terms for data access. Am I using ADO.NET ?? Is the methodology called Code Behind as opposed to something else? Are there better ways to connect to a database and Read/Update/Delete records.
Q3. If I want to use a Data Grid to show the data do I use the generated classes to do this - or just 'bind' the grid directly to the database?
I have been a developer for 40 years but the newer .Net models are causing me some confusion so any advice gratefully received. (and any simple books or tutorials that can be suggested would be helpful).
Thanks
Q1: What you want is to generate "EF Designer from database" by using a Entity Data Model.
In your project add a new item and look under "Data".
What this technique does is auto generating entity classes from your predefined database.
Q2: I suggest you read Getting started with Entity Framework. "Database First" is just a description of how you want to use the Entity framework - by saying you already have a database structure.
Q3: In this case if you use Entity Framework and the "database first" mechanism to generate entities you can databind the gridview from the result of a entity LINQ query, please see this example: Tutorial: Databinding

Dynamically update table rows in entity framework

Can anyone tell how to update table rows dynamically in entity framework? I am new in entity framework. Thanks in advance.
This is a pretty general question, but what you need to do is basically this:
Access your data context.
Get a given object (/record) from your context (/database).
Change the object.
Call SaveChanges() on the data context.
If you are learning EF a site that helped me a lot when I was learning it is http://msdn.microsoft.com/en-us/library/gg696194(VS.103).aspx. It contains lots of information in problem/solution format. You can also look at this book:
http://www.amazon.com/Programming-Entity-Framework-Building-Centric/dp/0596807260/ref=sr_1_1?ie=UTF8&qid=1355991856&sr=8-1&keywords=entity+framework
or this book:
http://www.amazon.com/Programming-Entity-Framework-Code-First/dp/1449312942/ref=sr_1_4?ie=UTF8&qid=1355991858&sr=8-4&keywords=entity+framework
which are both quite useful depending on whether you wish to use the designer or Code First.

Two identical dbml data context's in one asp.net application

I have a separate copy of a sql server2008 database that we used to isolate data. I would now like to bring a few records into the production database from this separate database. I am not sure of the best way to do this, but I need to transfer several tables and ensure the relations remain. The easiest way I think of is to (if possible) create another dbml data context of the separate database and set all variables equal to a new object in the new database. I realized that I am getting many conflict errors. Is there a way to have two databases coexist like this in one application? There a few minor differences in the databases. The production database does have a few added rows.
Thank you for the help,
Chris
I'm no authority so perhaps someone can give you a better solution but it seems you can't have two models in the same project with an identical table name. What you could do is create a new project in your solution just to contain the model for the separate database.
This will let you have two models that both contain a "person" object for example because their namespaces will be different. Might help to put using statements at the top of your pages that use both models similar to the below:
using PersonLive = MyProject.Person;
using PersonIsolation = AlternateProject.Person;

Is it possible to map an entity to the result of a stored procedure in Entity Framework?

I'm attempting to setup Entity Framework using Code First on an existing database. The database isnt in great shape (poor naming convention and some constraints are needed). The application I'm building is an MVC app. I have a "Model", "Repository", and "Web" (mvc) tiers.
To setup EF and map my model objects (POCO objects), is it required that I match my objects to database tables? Can I, instead, use my own stored procedures for CRUD operations? Would it help if I use WCF data services?
I'm planning on using fluent configurations to map my objects, but having some issues due to the database schema. I'm know considering redesigning the database just to get EF to map correctly. Any suggestions would be greatly appreciated!!
Looks awfully similar to this question:
Does Entity Framework Code First support stored procedures?
The answers there may be helpful to you, as well as the discussion surrounding how Code First and Stored Procedures don't make a whole lot of sense.
Wow, Julie Lerman answered!
Yes, it's possible, but not particularly easy. In particular, you must call context.Database.SqlQuery<T>() where T is the entity type you want to return, and your SQL must match up to that entity type. Otherwise, you will have to massage a result set into a type.
In general, you will have to do most of this manually, although you could probably figure out a T4 template to generate it for you, or maybe use some other tool like CodeSmith.

Many-to-many relationships in ASP.NET Dynamic Data

I need to use a many to many relationship in my project and since Dynamic Data doesn't support it natively, I would like to ask for a sample of an implementation of something like it.
I'm specifically looking for a way to both create new instances of the target entity and add a new m2m relationship to a record as well as using existing instances to create the relationship.
I'm on a tight schedule, so any help is really appreciated.
David Ebbo (the architect for Dynamic Data) posted a Many-to-Many field template on his blog. Note that this solution is specifically for Entity Framework.

Resources