Entity Framework. Updating EntityCollection Using Disconnected Objects Via Navigation Property?

Apr 22, 2010

I have a question, much liket this unanswered one. I'm trying to work with the entity framework, and having a tough time getting my foreign tables to update. I have something basically like this in the DB:

Incident (table):

-ID
-other fields

Responses (table):

-FK:Incident.ID
-other fields

And and entities that match:

Incident (entity)
-ID
-Other fields
-Responses (EntityCollection of Responses via navigation property)

Each Incident can have 0 or more responses.

In my Webpage, I have a form to allow the user to enter all the details of an Incident, including a list of responses. I can add everything to the database when a new Incident is created, however I'm having difficulty with editing the Incident.

When the page loads for edit, I populate the form and then store the responses in the viewstate. When the user changes the list of responses (adds one, deletes one or edits one). I store this back into the viewstate. Then when the user clicks the save button, I'd like to save the changes to the Incident and the Responses back to the DB. I cannot figure out how to get the responses from the detached viewstate into the Incident object so that they can be updated together.

Currently when the user clicks save, I'm getting the Incident to edit from the db, making changes to the Incident's fields and then saving it back to the DB. However I can't figure out how to have the detached list of responses from the viewstate attach to the Incident. I have tried the following without success:

Clearning the Incident.Responses collection and adding the ones from the viewstate back in:

Incident.Responses.Clear()
for each objResponse in Viewstate("Responses")
Incident.Responses.add(objResponse)
next
Creating an EntityCollection from my list and then assiging that to the Incident.Responses
Incident.Responses = EntityCollectionFromViewstateList
Iterating through the responses in Incident.Response and assigning the corresponding object from viewstate:
for each ObjResponse in Incident.Responses
objResponse = objCorrespondingModifedResonseFromViewState
Next

These all fail, I'd like to be able to merge the changes into the Inicdent object so that when the BLL calls SaveChanges on the changes to both the Incident and Responses will happen at the same time.

View 1 Replies


Similar Messages:

C# - Entity Framework - How To Use Navigation Property

Feb 2, 2011

I use EF 4 and C #.

I need populate a Pure Junction Table (containing only FK to other tables) when an EntityDataSource _Inserted event is raised.

I have 3 Tables in my Database:

[code]....

Here a useful resource:

But I am not able to implment it:

[code]....

View 1 Replies

Entity Framework 4 - Check If Navigation Property Collection Is Empty, Without Include() Or Load()?

Sep 28, 2010

In an MVC view, I'm looking for a way to determine if a parent entity's collection of child entities is empty, so I can decide if I need to call a RenderPartial() or not.

For a one-to-one I've been using the following:

<% if (Model.Book.GenreReference.EntityKey != null) %>
{.....}

but I'm unsure how to do it in a one-to-many scenario, or if it can even be done without the use of Include() or Load().

View 2 Replies

Adding To EntityCollection Adds To The End Of Collection In Entity Framework?

Feb 28, 2011

I'm trying to have a View where the user can add items in a collection without having to go to a new View (the scenario is a sort of CV site where the user adds info about work experience, skills, etc, and it would seem absurd to go to a new View to add each little thing).

So I have an edit View that shows a number of text boxes for the already added items, and there's an ajax call to go to a method to fetch the collection fresh if the user adds an item.

Here are the methods in question:

[code]....

View 1 Replies

Implementing Navigation Properties In Entity Framework?

Oct 1, 2010

I've been learning MVC 2 and I have pretty much everything understood except for the model part of things, I understand what the model is but actually implementing it has me confused.

Here's my situation, I have my DB which has 3 tables;

Ideas - table of ideas
Tags - table of tags
IdeaTag - link table connecting the above 2 tables via FKs

So when using the Entity Framework (.edmx) designer in VS2010 I get 2 classes created in the Designer, which obviously map to my DB tables and Navigation Properties in Idea for Tags & Idea for Tag.

So this is all fine until I actually try to add tags to an idea, what's the best practise for dealing with Navigation Properties? I wanted to add a Textbox which will then map to the Tags property in the Idea class but I'm unsure how I'd go about this.

Most of the MVC tutorials which discuss EF or Linq to SQL are quite basic.

View 1 Replies

Databinding Exception With Entity Navigation Property?

Mar 4, 2010

I have two Entity classes: Order and OrderItem. Order contains a navigation property OrderItemSet of type

System.Data.Objects.DataClasses.EntityCollection<OrderItem>

On an aspx page is a FormView bound to this EntityDataSource:

[code]....

When I run the application I get an exception pointing to the line DataSource='<%# Eval("OrderItemSet")%>' in markup and telling me:

DataBinding: System.Web.UI.WebControls.EntityDataSourceWrapper does not contain a property with name 'OrderItemSet'

What is wrong here?

View 1 Replies

ADO.NET :: Gen Objects Mapped To Entity Framework?

Dec 6, 2010

I've been tasked to convert our old DAL that have gen objects to possibly the Entity Framework 4.0. We create objects inheriting from our genned objects and have a ton methods our legacy codes uses. Does anyone have an idea or possible solution on how to create an entity framework model that maps to our old genned objects? Or a solution that requires us to modify our legacy code minimally?

View 1 Replies

Entity Framework Getting Objects Not Immediately Related?

Jun 24, 2010

I am using Entity Framework for my .NET application. I have been able to return objects and their directly-related objects as well (very convenient), but I am having trouble getting the objects of those objects.

IEnumerable<Lot> i = (((ObjectSet<Car>)_carRepository.GetQuery()) .Include(a => a.CarTypes).Take(10).ToList()

This works and I can access carTypes, however I cannot figure out how to access tables associated with CarTypes (e.g. tables which have fields associated with the car types). I tried to use a Join however I was unable to figure out how to get it to work right.

View 2 Replies

Use FormView In Order To Insert Complex Entity Framework Objects?

Jun 11, 2010

I'm trying to use formview in order to do insert of a new entity object (called Customer) Customer has a reference to another entity called Address. How can I fill both of them in the same formview?

View 1 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

C# - Best Way To Edit And Update Complex Viewmodel Objects Using Mvc2 And Entity Framework?

Apr 2, 2010

I have a table in my database with a one to many relationship to another table, which has a relationship to a third table:

[Code].....

This seems to work fine. My question to you good folks: Is there a correct way to do this? I tried following the making a custom model binder per the blog link I posted above but it didn't work (there was an issue with reflection, the code expected certain properties to exist) and I needed to get something going ASAP. PS - I tried to cleanup the code to hide specific information, so beware I may have hosed something up.

View 1 Replies

C# - .NET Remoting - How To Handle Disconnected Objects

Oct 26, 2010

First, let me explain a little bit about my setup. My server is setup as a Marshaled singleton object:

RemotingServices.Marshal(lsServer, ServerObject.objectUri);

In order to have two-way communication from the server to the client, the client activates a ServerObject, then passes in its own ClientObject (which is also a MarshalByRefObject) the server uses the ClientObject to talk back to the client:

var lsClient = new ClientObject();
var lsServer = (ServerObject)Activator.GetObject(typeof(ServerObject), url);
lsServer.attach(lsClient);

When the attach method is called, the server takes the ClientObject and stores it in a List:

public void attach(ClientObject client) {
clientList.Add(client);
}

The server can then call a method on the ClientObject called "SendMessage" to communicate to each client in its list:

foreach(var c in clientList) {
c.SendMessage("Hello");
}

This works great until a client is encountered that has disconnected. In this case, an error is thrown about the client refusing the connection. I can catch the error, but the problem is I cannot remove the reference to the ClientObject from the server's List. The following re-throws the "connection refused" exception anytime a client has been disconnected:

clientList.Remove(badClient);

So a couple of questions:

1- Is there a way to detect the connection state of a client without having to catch an error?

2- When I do detect a client that has disconnected, why does the attempt to remove the object from the server's list appear to "communicate" with the client (thus causing the exception to be thrown again?)

EDIT

As Patrick pointed out below, doing a Remove from a list causes the List to iterate my objects and do a comparison on them (calling .Equals) - this is what caused my ClientObject to attempt to communicate again and thus throw the exception. To resolve it, I instead changed the List to a dictionary keyed off a unique ID supplied by my Client at the time of attaching to the server. Then I used that unique ID to remove the client. I never did find a direct way for determining if a client had disconnected other than catching the error.

View 1 Replies

Error While Updating Database Record With Entity Framework On MVC Page?

Mar 12, 2010

I have an ASP.NET Page that updates registered User Address Details for a selected record.

Below is the Update method that i am calling from Controller.

When i am calling ApplyPropertyChanges method, I am getting the below error.

Did anyone run into the same error while updating the record with Entity Framework.

[code]...

View 4 Replies

C# - Adding A Custom Property To Entity Framework?

Oct 19, 2010

I am using the Entity Framework for the first time and want to know if the following is possible - I have generated my classes from the DB, and have one called Category.

Obviously it has all my fields in the table (ID, CategoryName, SortOrder etc..) but I want to know if I can add a custom property which is not in the table, but is actually the result of a custom method.

I want to add a new property called 'CategoryURL' which is basically the 'CategoryName' property run through a custom method and returns a hyphenated string.

My initial thought is inheriting from the generated Category class and creating something like this inside?

public string CategoryURL
{
get{ return MyCustomMethod(this.CategoryName) }
}

Is this the correct approach? And will 'this.CategoryName' work as I think it should? Basically the end result is when I return a list of 'Category' I want this to be part of the class so I can use it in my foreach loop.

View 1 Replies

C# - Assign EntityCollection Property To Another Property?

Nov 19, 2010

I have a type that is derived from an Entity generated by the Entity Framework 3. How do I assign one Customer's Order property to the Derived Customer's Order property?

derivedCustomer.Orders = customer.Orders

I'm not actually trying to swap orders; this is just an example of what I am trying to achieve. Has anyone done tried this and succeed?

View 1 Replies

DataSource Controls :: Foreign Key Reference Not Updating With ADO.NET Entity Framework Silverlight/Data Service

Apr 8, 2010

I'm trying to insert a new EF object containing a reference to a related table via foreign key, and cannot get the reference stored in the new object. I have two tables in a SQL Server 2008 Database:

Component Templates
PartGroups

with a Foreign Key linking them thus: ComponentTemplates:ID [0..1<->*] PartGroups:ComponentTemplateID

I have imported these tables into a .NET web application project .edmx file (using a new ADO.NET Entity Data Model), and then created a Data Service which I then reference in a Silverlight 3.0 project. The objects are available as expected in the data context called by my Silverlight app and load correctly. Now, when I try to save a reference of ComponentTemplate into a newly created PartGroup, the PartGroup object is created in the database, but the value of ComponentTemplateID in the newly created table record is NULL.

[Code]....

(Note that _ctCtrl.ComponentTemplate is a confirmed non-NULL object reference to the parent object on which PartGroup is a child)

View 2 Replies

Exporting Entity Framework 4 Data Model To Entity Framework 3.5?

Oct 25, 2010

Is there a way to export a EF 4.0 Data Model to EF 3.5?

I looked around and found that we are not able to access EF 4.0 from a ASP.Net 3.5 project here: [URL]

Our project is the 1st to go to .Net 4.0 using Entity Framework and we (the team) were wondering if there was a way for the other projects that "might" need to access our data that are still using the .Net 3.5 framework.

View 1 Replies

MVC :: Using Data From Multiple Entity Framework4 Model Entity Objects In MVC2 Controller?

Sep 29, 2010

getting data from multiple Entities models into an MVC controller. Most of the examples I have seen for using EF in an MVC2 app only use a single entitiy.

I have just started using MVC2 in C# using the Entity Framework models (CIOps.model) created from an SQL database. I can create the controller and views using single tables of the model in MVC2, but I just cannot get my head around how to get data from multiple entity tables into the controller (similar to joins in T-SQL). I have included an example below of the controller code that works with a single entity table, tbl_tours (tbl_tour in DB).

Could someone please illustrate how this code would be changed to include additional columns from FK tables in addition to tbl_tours columns. E.g. the clients name from the tbl_clients, the coordinators name from the tbl_employees, and costs from tbl_costs entities? Is it possible to do this directly using the EF model entities/classes that are already created and not use LINQ, POCO, Repositories, etc.? The FK relationships are already in the EF models. I have included the whole controller code, but I just need a few examples of how to join the multiple entities, not rewrite every CRUD function function in the controller. I think this will get me over the hump in using EF in an MVC2 App. Also ignore the fact I am using the home controller, this will change in the application.

[Code]....

View 21 Replies

ADO.NET :: Entity Framework With Stored Procedure Versus Entity Framework Without Stored Procedures

Nov 2, 2010

I am new to entity framework , it is really very good , but I want to know what is the difference between using entity framework with stored procedure or without stored procedure , which one the faster and what is the benefits for using stored procedures with entity framework.

View 1 Replies

Entity Framework -Update Entity When Another Entity Is Updated

Jun 30, 2010

I have an EDM, it includes the entities extension and history. My goal is to use history to keep track of all the changes made to extension entity. For example, if extension with ID 223 has its property 'Name_Display' changed - I want the history entity to record this.

I'm using ASP.NET with VB.NET. Where in my code do I put the hook to say, "update the history entity" and what should that hook look like?

View 1 Replies

Cast Exception When Try To Insert Entity In Entity Framework (using Code-f)

Feb 9, 2011

I get an cast exception when i am trying to insert an entity in Entity Framework (using code-first). From this code :

public virtual T Insert(T entity)
{
return Context.Set<T>().Add(entity);
}

The cast exception is like "impossible to cast ...Collection'1(Entity) to type (Entity)" I can't figure out why. I am pretty sure ive done everything right. Post entity

public class Post
{
public long PostId { get; private set; }
public DateTime date { get; set; }
[Required]
public string Subject { get; set; }
public User User { get; set; }
public Category Category { get; set; }
[Required]
public string Body { get; set; }
public virtual ICollection<Tag> Tags { get; private set; }
public Post()
{
Category = new Category();
if (Tags == null)
Tags = new Collection<Tag>();
}................................

View 9 Replies

ADO.NET :: Entity Framework - Entity Stored Procedure Mapping - Can't Update

Feb 10, 2011

I am mapping a stored procedure to an entity by right clicking on the entity (in the .edmx) and selecting "Stored Procedure Mapping." This brings you to a Mapping Details - "Name of Entity" Window that allows you to select the insert, update, and delete stored procedures associated with the Entity. It also maps the stored procedure parameter to the Entity "Property" (Column).

I'm gettin an error "error 2042: Parameter Mapping specified is not valid." The cause of the error is fairly obvious, in the Insert stored procedure that has been selected, a 'CHAR' parameter is being mapped to an Int32 Entity Property. I altered the stored procedure parameter to match the entity, I deleted the stored procedure, readded, and reslected it as the Insert function. I also cleaned, validated, updated model from database. No matter what I do, the parameter list in the mapping details doesn't reflect the change to the stored procedure. It's stuck on a char --> int32 mapping, even though it has been changed, like it's buried deep in meta data some where.

View 2 Replies

ADO.NET :: Adding A New Entity In Entity Framework 3.5 With Related Data?

Aug 5, 2010

I am trying to add a new entity and have to refernce associated data to add it. I cannot load the Referencetables. Giving "The EntityReference could not be loaded because it is not attached to an ObjectContext." How do i complete this task in Entity Framework 3.5

[Code]....

View 1 Replies

ADO.NET :: Entity Framework Copy Entity And Change PK And Save Old And New

Mar 31, 2011

I have a stupid problem with the Entity Framework. I get the following Exception:

[Code]....

I have an entity with a 4 fields representing the primary key. I copy it via serialization (works fine). I set the old entity to not valid (Datefield set to a date in the past, this field is part of the PK) and set the copied entity to DateTime.Now.Date. When I call context.AddObject I get the Exception above. I tried copying the entity via reflection but the entity has 3-4 references. And when copied, I get another Exception before even Adding the entity to the context. I also tried setting newObj.EntityKey = null and reset all the fields neccessary for the PK. I just want a whole copy of an entity with a different primary key (and some other fields changed too) and Create it in the database.

View 1 Replies

Set The DataTextField To A Property Of A Navigation Property?

Jan 30, 2011

i get a list of objects from the Entity Framework data context.

var list = context.EntityA;

the EntityA is the main object (contains the primary key), but has a navigation property called "EntityALanguages", which contains language specific properties.

now i want to bind the list to a dropdownlist and need so set DataValueField and DataTextField properties from the dropdownlist.

how can i set the DataTextField to a property of a navigation property, something like:

this.ddl.DataValueField = "GUID";
this.ddl.DataTextField = "EntityALanguages.ShortDescription";

View 1 Replies







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