DataSource Controls :: Entity Framework - Parent And Child Relationships?

Feb 4, 2010

I am just getting started with entity framework with asp.net 3.5 using VS 2008

There are two tables, Categories & Subcategories:

Category table: CID, CategoryName Sub Category table: SID, CID, SubCategoryName I would like to get SID, CID, CategoryName, SubCategoryName in the gridview. Just as we write a select statement i.eSelect a.sid, b.cid, b.categoryname, b.subcategoryname from subcategories a inner join categories b on a.cid=b.cid & put the same in a gridview.

I was trying for the whole day & couldnt get atleast one url that would teach how to do it.

View 3 Replies


Similar Messages:

Deleting Entity With Child Relationships

Apr 10, 2010

EF is so frustrating. I can't seem to be able to get my head around what I need to do so I can delete an object. I seem to be able to remove the object but not the related child objects. Can anyone tell me what is the rule of thumb when you want to delete all related child objects of a given object? I've tried loading all related objects like this:

Entry entry = ModelContext.GetObjectByKey(new EntityKey("ModelContainer.EntrySet", "Id", id)) as Entry;
entry.ChildEnteries.Load();
if (entry != null)
{
ModelContext.DeleteObject(entry);
ModelContext.SaveChanges();
}

I get errors related to the relationships: A relationship is being added or deleted from an AssociationSet 'EntryEntry'. With cardinality constraints, a corresponding 'Entry1' must also be added or deleted. Why can't I just load the object using modelcontext.GetObjectByKey and remove it along with its child objects? My other question is can I delete an object using Entity command like so? DELETE e from objectset as e where e.id = 1 I've tried few variations and all of them throw exceptions.

View 1 Replies

C# - In LINQtoSQL, How To Update Child Relationships By Editing The Parent Model

Oct 5, 2010

This is my first attempt at updating a database using LINQtoSQL. At least, it's my first if you don't count the tutorials I've followed. Unfortunately, The tutorials that I have found don't offer much more than updating a single table. I'm attempting to update a DB Model that's a bit more complex.

I have a Stream table:

[Code]....

View 1 Replies

ADO.NET :: Entity Framework - Query Based On Relationships?

Oct 27, 2010

I have the following related tables:

Users
Id
Username
Email
Password
UserStatuses
Id
Name
Users_UserStatus
Id
User (FK - Users: Id)
Status (FK - UserStatuses: Id)
StatusDate

As you can see, UserStatuses is a reference table with various statuses. Users_UserStatus is a join table between Users and UserStatuses. Using Entity Framework, how can I do a conditional to check if the latest StatusDate is "Pending"? As of now, I have the following:

[Code]....

View 1 Replies

Perform Relationships In Entity Framework 4 Code-First CTP 5 ?

Jan 19, 2011

I would like to have a short example on how do you actually perform relationships in Entity Framework 4 Code-First CTP 5 ?

Would love an example for these kind of relations :

* one-to-many
* many-to-many

View 1 Replies

Ms Entity Framework Delete Child Object

Mar 10, 2010

i have 2 tables without any cascade deletind. i wont to delete parent object with all child objects. i do like this

//get parent object
return _dataContext.Menu.Include("ChildMenu").Include("ParentMenu").Include("Pictures").FirstOrDefault(m => m.MenuId == id);
//then i loop all child objects
var picList = (List<Picture>)menu.Pictures.ToList();
//foreach (var item in menu.Pictures)
for (int i = 0; i < picList.Count; i++)
{
if (File.Exists(HttpContext.Current.Server.MapPath(picList[i].ImgPath)))
{
File.Delete(HttpContext.Current.Server.MapPath(picList[i].ImgPath));
}
if (File.Exists(HttpContext.Current.Server.MapPath(picList[i].ThumbPath)))
{
File.Delete(HttpContext.Current.Server.MapPath(picList[i].ThumbPath));
}
//**what must i do here?**
//menu.Pictures.Remove(picList[i]);
// DataManager dm = new DataManager();
// dm.Picture.Delete(picList[i].Id);
//menu.Pictures.de
//_dataContext.SaveChanges();
//picList[i] = null;
}
//delete parent object
_dataContext.DeleteObject(_dataContext.Menu.Include("ChildMenu").Include("ParentMenu").Include("Pictures").FirstOrDefault(m => m.MenuId == id););
_dataContext.SaveChanges();

View 1 Replies

DataSource Controls :: Parent Child Table In Sql?

Feb 26, 2010

I have a table that needs sorting by its hierarchial structure of parent-child.

An example of my table is below

ID
Name
Parent ID
Sort Order
1
iPhone Dev
0
1
2
iPhone Lounge
0
2
3
Web Development
0
3
17
Javascript
3
1
24
CSS
3
2
25
prototypes
17
1

As you can see above, my table structure follows this pattern

[Code]....

My overall aim is to have the sql order the table for me, and by using this new "depth" column, I will be able to indent by categories in the menu on the front end, you have probably seen this before, but all the tutorials I have read dont really explain it very well, or assume you are sql savvy.

View 2 Replies

DataSource Controls :: How To Delete Parent And Child Record

Jun 4, 2010

I have a table that have LibraryID and LibraryParentID.

LibraryID int Unchecked
Name varchar(50) Checked
Description varchar(200) Checked
LibraryLevel int Checked
LibraryParentID int Checked
LibraryTypeID int Unchecked

I want to write a sql to delete the currrent select parent and well as all child record, how can I do that?

View 4 Replies

Entity Framework - Collection Of Complex Child Objects In MVC 3 Application?

Feb 20, 2011

I want to be able to update a model and all its collections of child objects in the same view. I have been referred to these examples: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx and http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/ .

For example, I have an object Consultant, that has a collection of "WorkExperiences". All this is in an Entity Framework model. In the view, the simple properties of the Consultant object is no problem, but the collection I cannot get a textbox to show up for. I tried following the examples in the links above, but it doesn't work. The problem is, in those examples the model is just a list (not an object with a child list property). And also, the model again is an EF model. And for some reason that doesn't seem to work as in those examples.

[Code]....

This stuff with the EditorTemplate works fine in Phil Haack's sample project, which I downloaded to try, but here, with the EF model or whatever the problem is, I don't get any textbox at all. The table in the view is just there as a test, because in the table I do get the rows for WorkExperiences, whether I add an empty WorkExperience object or fill out its properties doesn't matter, the rows show up for each object. But again, no textbox...

View 2 Replies

DataSource Controls :: Update/insert Parent Child At The Same Time?

Sep 3, 2010

Is there a way to create a contact form that takes the name. address and other info for the master record; Then, somehow, on the same page, allow the user to enter multiple contact info as chid records (i.e. phone, email, etc.)

All this using the Objec DataSource or SQL DataSource (which ever is easy) and doesn't require writing too much code.

View 1 Replies

DataSource Controls :: Stored Procedure For Delete Records From Parent And Child Table

Jan 16, 2010

I have two tables dept and employees those table have parent and child relation ship link column is deptno i want to delte department even this departmetn contains employees i want to write storedprocedure for first delte employees and after department if i wrtie these two querys in storedprocedure will i get any problems

Begin
Delete from tbl_employees where Department_Id=@Department_Id
Delete from tbl_Department where Department_Id=@Department_Id
RETURN
END

View 5 Replies

Visually Set Controls Datasource From Entity Framework?

Feb 27, 2011

I set up Entity Framework for my Table and Stored Procedure to return data. Created a Domain Class as well.

In Domain Class, I am returning Objects as collection.

On .Aspnet page when I drop a Control(Combo,Dropdown) I can set up the source for the controls visually if the source is table.

However, In domain class I have stored precedure object that return collection of data And I cant not set up visually from Designer, I am only available to write code behind. is it possibble that i can set up from desingner.

View 6 Replies

DataSource Controls :: WEB Services And Entity Framework?

Jan 19, 2010

CAN we use Entity framework in web services.I the following error while the code connects to the database using entity framework

View 3 Replies

DataSource Controls :: Uniqueidentifiers And Entity Framework?

Feb 16, 2010

I have a sql server database that includes the field ArticleID of type uniqueidentifier that is a primary key and is set to be auto-generated by the database when a new article is added.If i generate my entity model and do the following code:

[Code]....

Then a new article is saved to the database but the entity framework must be generating a new blank Guid and saving that to the database rather than allowing it to be auto generated as the articleid that shows up saved is all 0.How can i get the Entity Framework to let the Database generate the ArticleId?

View 3 Replies

DataSource Controls :: How To Use A Stored Procedure In Entity FrameWork

Mar 22, 2010

i joined together more than one entity: Customer, Personel etc. i used inner join structures. if i look adonet entity framework ,5 entities are generated. i need to run this SP in C# side:

[Code]....

LOOK BELOW PİC : you can understand it :

MY C# codes: [Code]....

How can i get all joined data from sql with using randevu.GetRandevular(customerID

View 1 Replies

DataSource Controls :: Entity Framework Deleting Many To Many Relationship?

Jul 9, 2010

I have 3 tables User, role and UserInRole THe entity frame work made entities for User and Role and I have successfully made a join with the user and role entities using user.add(role); but I can't seem to get it to remove the link (I want to retain the user and role just remove their association).

using (ProviderEntities context = new ProviderEntities())
{
User u = context.User
.Where(n => n.ApplicationName == application)
.Where(n => n.Username == username).First();
Roles r = context.Roles
.Where(n => n.ApplicationName == application)
.Where(n => n.RoleName == roleName)
.First();
r.User.Remove(u);
context.SaveChanges();
}

The oposite worked when I did r.User.Add(u); but it wont let me now remove it after it been created. Driving me mad as I can't see where I have gone wrong.

View 1 Replies

DataSource Controls :: Use Database Instead Of MDF File With Entity Framework?

Jan 11, 2010

I transfered my data from MDF files to MS SQL Database, but I can't get it working with entity framework.

My Old connection string is:

metadata=res://*/Models.EntityModel.csdl|res://*/Models.EntityModel.ssdl|res://*/Models.EntityModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.SQLEXPRESS;AttachDbFilename=|DataDirectory|TripsDB.mdf;Integrated Security=True;User
Instance=True;MultipleActiveResultSets=True"

I put all data from TripsDB.mdf to Trips table in local MS SQL 2008 Enterprise database. Also changed connection string to:

Server=WIESIEK;Database=Trips;Trusted_Connection=True;providerName=System.Data.SqlClient;User ID=Jan J. Roman

But my application throws System.ArgumentExceptio with message: The 'server' keyword is not supported.

on line:

private TripsDBEntities2 _entity = new TripsDBEntities2();where TripsDBEntities2 is Entity Framework instance.

View 2 Replies

DataSource Controls :: Stored Procedure In Entity Framework?

Jul 7, 2010

I have a stored procedure that generates a float value. I've added the stored procedure to my edmx file and created a function import, but the function isn't showing up in the object browser or via intellisense.

I'm using Visual Studio 2010 and I've verified that the stored procedure works in SSMS. Here is the current stored proc I'm using:

[Code]....

View 1 Replies

DataSource Controls :: Entity Framework EntityKey (Defaults To 0?)

Feb 6, 2010

CarType
Id - PK
Name
CarDetail
Id - PK
TypeId - FK to CarType

I have this simple example here and I want to insert a new CarDetail into the database but for some reason I cannot get the EntityKey to work in EF4 and Framework4 beta 2.

If i declare the following

car_detail.CarTypeReference.EntityKey = new System.Data.EntityKey("MyDB.CarType", "Id", 1)

Each time i try to call SaveChanges, it throws a INSERT constraint error saying that there is a conflict between CarType and CarDetail with the FK. It just seems like the hard coded 1 for CarType is NOT being passed through but instead the default value of 0. What is going on?

If i call car_detail.TypeId = 1 then it works fine with the INSERT.

Why doesn't the EntityKey method work?

View 1 Replies

DataSource Controls :: Left Join With Entity Framework?

May 17, 2010

how to do this query in EF1:

[Code]....

View 3 Replies

DataSource Controls :: Viewing Fields From Two Tables Using Entity Framework

Mar 17, 2010

I have created a .edmx for the northwind database. When I run the following statement, the correct number of detail records are retrieved and I can access the detail fields, but how do I access the order header record to view OrderDate, ShipAddress, etc...?

[Code]....

View 1 Replies

DataSource Controls :: How To Save Multiple Rows Using ADO.NET Entity Framework

Mar 23, 2010

public void saveOptionalCourses( ArrayList coursesAdded, int studentID)
{
StudentOptionalCoursesXRef XRefObject = new StudentOptionalCoursesXRef();
using (var EntityObject = new SchoolProjectEntities2())
{
foreach ( string list in coursesAdded)
{
XRefObject.StudentDetails = EntityObject.StudentDetails.Where(c => c.StudentID == studentID).First();
XRefObject.OptionalCourses = EntityObject.OptionalCourses.Where(c => c.OptionalCourseName == list).FirstOrDefault();
EntityObject.AddToStudentOptionalCoursesXRef(XRefObject);
}
EntityObject.SaveChanges();
}

with this above method and could save only last row that am returning remaining rows are iterated but are not saving on to database

View 2 Replies

DataSource Controls :: How To Insert A Null Into A Record Entity Framework

Feb 24, 2010

I'm using the linq to entity command to perform a record update. I want to set one of the fields back to NULL but I'm not sure how do it. I have a query such as

Dim record = From r in db.LISTS where r.ID = refID Select r
record.Title = txtTitle.text
if txtDetail.text = "" then
record.Detail =
end if

View 3 Replies

DataSource Controls :: Entity Framework And Architecture Of Model - Create Application Using EF

Feb 10, 2010

I can't to understand clearly how to create strong and quickly application using EF. For example, I have class ForumPost (table ForumPost) for select one of record I write method like:

public ForumPost ForumPost(int ForumPostID)
{
return
(from i in _dataContext.ForumPostSet
where i.ForumPostID == ForumPostID select i).First();
}

all works. But on page (I use ASP.NET MVC) I should display info from another linked tables, ForumName (each post have ForumID) etc. So, I modify this method to:

public ForumPost ForumPost(int ForumPostID)
{
return
(from i in _dataContext.ForumPostSet
.Include("ChildPosts").Include("ParentPost").Include("Users").Include("Forums").Include("Tags").Include("ForumPostPolls")
where i.ForumPostID == ForumPostID select i).First();
}

ok, all works. Then I want to make some actions in model with ForumPost, i.e.

public void RootPost(int PostID, ref Models.ForumPost Root)
{
ForumPost post = ForumPost(PostID);
if (post.RootPost != null)
{
Root = ForumPost(post.RootPost.ForumPostID);
}
else
Root = post;
}

it works too. But problem is in speed. I not need in RootPost all those includes. So, I should to create one more method like:

private ForumPost ForumPostIncludes1(int ForumPostID)
{
return
(from i in _dataContext.ForumPostSet
.Include("ChildPosts")
where i.ForumPostID == ForumPostID
select i).First();
}

then ForumPostIncludes2, ForumPostIncludes3 etc... second way - redesign RootPost with LINQ expr, not with call another method. But in this way difficulties to change DB. third way? How to do it correctly?

View 1 Replies

DataSource Controls :: Best Way To Handle Inserts For Entities When Using FormViews And Entity Framework 4?

Mar 3, 2010

I am trying to get some guidance with this issue:

I have a Customer object that has two address ID fields (one for HomeAddress and one for WorkAddress)

I am loading the Address Objects by using Include="HomeAddress, WorkAddress" and then binding to the properties using the Navigation Property on the Customer Entity

Can I use just one FormView for doing the Insert/Updates and binding it to an EntityDataSource or would I have to do all the Inserting/Updating manually?

View 1 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved