Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4.5 > BOOK: Beginning ASP.NET 4.5 : in C# and VB
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning ASP.NET 4.5 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4.5: in C# and VB by Imar Spaanjaars; ISBN: 978-1-118-31180-6
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4.5 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 8th, 2014, 05:19 AM
Registered User
Points: 16, Level: 1
Points: 16, Level: 1 Points: 16, Level: 1 Points: 16, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2014
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Entity Framework update problem

Hi When i add delete a record in entity framework it works fine. Also i can also update primary key tables. But there is a problem update tables with foreign key. Schedule Entity for example is updated fine. But required entity not working. why?? i get this issue "the property 'roomType is part of the object's key information and cannot be modified entity framework " Thank you
Here is my Code:
Using myEntities As New FacultySchedulingSystemEntities
ScheduleEntity = New Schedule

ScheduleEntity = (From mySchedule In myEntities.Schedules
Where mySchedule.CourseCode = CourseCodeString
Select mySchedule).SingleOrDefault
ScheduleEntity.CourseName = CourseNameTextBox.Text
If IsRequiredRadioButtonList.SelectedItem.ToString = "Yes" Then
ScheduleEntity.IsRequired = True
Else
ScheduleEntity.IsRequired = False
End If


RequiredEntity = (From myRequired In myEntities.Requireds
Where myRequired.CourseCode =(CourseCodeString)
Select myRequired).SingleorDefault

RequiredEntity.RoomType = (RoomTypeRadioButtonList.SelectedItem).ToString


myEntities.SaveChanges()


End Using

Last edited by melis_13nic; April 8th, 2014 at 05:35 AM..
Reply With Quote
  #2 (permalink)  
Old April 8th, 2014, 10:18 AM
Authorized User
Points: 416, Level: 7
Points: 416, Level: 7 Points: 416, Level: 7 Points: 416, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2013
Posts: 84
Thanks: 29
Thanked 2 Times in 2 Posts
Default

Hi,

I'm sure Imar will comment, but I don't recognize your example from this book, "Beginning ASP.NET 4.5: in C# and VB". Is it possible your example is from another book and you posted here by accident?

Not recognizing the example and being unable to see the rest of the context, it's difficult to understand exactly the problem is or immediately see what might be the problem.

Best Regards,
Alan
Reply With Quote
  #3 (permalink)  
Old April 8th, 2014, 11:37 AM
Registered User
Points: 16, Level: 1
Points: 16, Level: 1 Points: 16, Level: 1 Points: 16, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2014
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Alan thank you for your quick reply its an example for my application but i follow the book's guideline. So i found that the problem is i cant update a record where is a part of a composite key. The only way i found so far is by deleting the record and add it again. So my question is if there is another way to solve this issue. Thank you
Reply With Quote
  #4 (permalink)  
Old April 8th, 2014, 12:26 PM
Authorized User
Points: 416, Level: 7
Points: 416, Level: 7 Points: 416, Level: 7 Points: 416, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2013
Posts: 84
Thanks: 29
Thanked 2 Times in 2 Posts
Default

Hi Melis(?),

I'm not that great at this and it would be useful to see your full declarations / code in this area. But I'm not sure that's necessary to figure out the problem.

You have this object myEntities that is type FacultySchedulingSystemEntities. But I don't see where myEntities is actually set to anything before you start using it. I'm not sure you can save the changes with something that is never assigned values. Actually I don't see how the query will work before myEntities is assigned values. Doesn't there need to be an assignment after myEntities is declared as a New type?

Maybe I'm just missing something because I'm too new at this?

Best Regards,
Alan
Reply With Quote
  #5 (permalink)  
Old April 8th, 2014, 01:00 PM
Registered User
Points: 16, Level: 1
Points: 16, Level: 1 Points: 16, Level: 1 Points: 16, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2014
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Alan i have made all the neccessary assignments but i dint post the whole code here. The problem is that i cannot modify the roomtype attribute of a record because roomtype is part of the primary key in the table. Thank you for your help i appreciate it
Reply With Quote
  #6 (permalink)  
Old April 8th, 2014, 02:17 PM
Authorized User
Points: 416, Level: 7
Points: 416, Level: 7 Points: 416, Level: 7 Points: 416, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2013
Posts: 84
Thanks: 29
Thanked 2 Times in 2 Posts
Default

Hi,

OK, now I understand.

I believe it's a fundamental premise that you can't change the primary key in a database record. This is getting into database design. If you have a need to change data that is the primary key in a record, then a different database design is warranted, as disappointing as that may sound.

There may be some unusual exception to this, but in general I think the database structure needs to be looked at.

Best Regards,
Alan

Last edited by AlanWheeler; April 8th, 2014 at 02:20 PM..
Reply With Quote
  #7 (permalink)  
Old April 11th, 2014, 03:35 AM
Imar's Avatar
Wrox Author
Points: 72,038, Level: 100
Points: 72,038, Level: 100 Points: 72,038, Level: 100 Points: 72,038, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,080
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Changing your primary is (most likely) a very bad idea. For some background, take a look here: http://stackoverflow.com/questions/1...tity-framework

That said, you have a few alternatives:

1. Add a surrogate key to the table. E.g. introduce something like an Id (Identity) column that doesn't change. Then your other fields can freely be updated.

2. Manually update all the foreign key references and the primary key and refresh EF. Someone in the above thread suggested to write a stored procedure to do this.

Personally, I would look into option 1 as it'll make things much more manageable.

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!
Reply With Quote
  #8 (permalink)  
Old April 11th, 2014, 06:39 AM
Registered User
Points: 16, Level: 1
Points: 16, Level: 1 Points: 16, Level: 1 Points: 16, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2014
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you very much Imar for your help. You are the best.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
using entity framework 5 misuk11 BOOK: Professional ASP.NET Design Patterns 1 February 25th, 2014 05:23 PM
Entity Framework baxterwlane@hotmail.com BOOK: Professional Sitecore Development 0 January 3rd, 2014 12:40 PM
Entity Framework vbboyd BOOK: Beginning ASP.NET 4.5 : in C# and VB 5 January 18th, 2013 11:48 AM
databind problem with templateField in details view - related to entity framework LesBerg ADO.NET 3 August 26th, 2009 12:49 PM



All times are GMT -4. The time now is 12:02 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.