Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > .NET > Other .NET > BOOK: Professional LINQ
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Professional LINQ
This is the forum to discuss the Wrox book Professional LINQ by Scott Klein; ISBN: 9780470041819
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional LINQ 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
 
 
Thread Tools Display Modes
  #1 (permalink)  
Old September 10th, 2009, 11:51 AM
Authorized User
 
Join Date: Sep 2009
Location: Everett, WA
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
Exclamation It has a primary key!

I hope I am not missing something obvious here. When trying to make modifications to a database using linq, you must have a primary key. My database has a primary key and I have identified it in the class.
Code:
[Table(Name = "Accounts")]
        public class AllAccounts
        {
            [Column(DbType = "int not null, IsPrimaryKey=true, IsDBGenerated=true")]
            public int AccountID;
However when I atempt to insert data, I get the following error.
"Can't perform Create, Update or Delete operations on 'Table(AllAccounts)' because it has no primary key."

Code:
public class DRCACCT : DataContext
        {
            public DRCACCT(string connection) : base(connection) { }
            //table definitions
            public Table<VItems> Items;
            public Table<AllAccounts> AllAccount;//Account
        }
Code:
DRCData.AllAccounts pacct = new DRCData.AllAccounts();
                        
                        pacct.IsValid = true;
                        pacct.ApplicationOwner = "DRCTECH";
                        pacct.AccountType = 6;
                        pacct.Name = User.Identity.Name + DateTime.Now.ToString();//tbDescription.Text;
                        pacct.Active = true;
                        pacct.CreatedBy = User.Identity.Name;
                        pacct.CreatedDateTime = DateTime.Now.ToShortDateString();
                        pacct.OpeningBalanceDate = DateTime.Now.ToShortDateString();
                        pacct.OpeningBalance = 0;
                        db.AllAccount.InsertOnSubmit(pacct);
                        db.SubmitChanges();
I thought this was working at one point, but in trying to solve another problem, I removed it and then put it back. I point this out because I am using VS2008 and have noticed that sometimes changes I have made to my code does not get updated somewhere in the vsguts. So perhaps, somewhere in the depths of VS this code is not what it appears to be. Or perhaps there is just something wrong with the code itself and I have overlooked it. Any help would be appreciated.
  #2 (permalink)  
Old September 10th, 2009, 01:34 PM
SQLScott's Avatar
Wrox Author
 
Join Date: Dec 2004
Location: Wellington, FL , USA.
Posts: 338
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Two things come to mind. First, I would regen the model. Something probably got whacked when you were making changes and it thinks there is no primary (identity) key. Second, did you make any changes to the table itself? I ask only because you state "I removed it and then put it back". Are you refering to the table itself or the underlying class (the code you show above)?

I mention these only to make sure all angles are checked. If the table does have a primary key, then I would regen the model only because it thinks no primary key exists even though you are telling it there is one. So, either it really doesn't exist in the table or the model is out of sync.

Please keep me posted.

Scott
__________________
========================
Scott Klein
Author of:
Professional SQL Server 2005 XML
Professional WCF Programming: .NET Development with the Windows Communication Foundation
Professional LINQ
========================
The Following User Says Thank You to SQLScott For This Useful Post:
DRCTECH (September 11th, 2009)
  #3 (permalink)  
Old September 10th, 2009, 02:49 PM
Authorized User
 
Join Date: Sep 2009
Location: Everett, WA
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
Question Regenerate

Yes, the table itself does have the primary key and identity specification assigned to AccountID. I did not use OR or Sqlmetal to define the entity, does regeneration still apply?
  #4 (permalink)  
Old September 10th, 2009, 04:05 PM
SQLScott's Avatar
Wrox Author
 
Join Date: Dec 2004
Location: Wellington, FL , USA.
Posts: 338
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Was any of it generated via OR or Sqlmetal? Meaning, from your last post, it sounds like you generated the model via OR or SqlMetal but added the Account entity manually. I know that is doable but I think there is a bit more than that.

Regen is still an issue but it will overwrite any custom code you have. If that isn't a concern, regen is still an option.
__________________
========================
Scott Klein
Author of:
Professional SQL Server 2005 XML
Professional WCF Programming: .NET Development with the Windows Communication Foundation
Professional LINQ
========================
  #5 (permalink)  
Old September 10th, 2009, 04:06 PM
SQLScott's Avatar
Wrox Author
 
Join Date: Dec 2004
Location: Wellington, FL , USA.
Posts: 338
Thanks: 0
Thanked 2 Times in 2 Posts
Default

A quick test would be to create a completely seperate VS 2008 project and gen the model via the OR and run the same code. That should be a fairly quick test and provide some insight...
__________________
========================
Scott Klein
Author of:
Professional SQL Server 2005 XML
Professional WCF Programming: .NET Development with the Windows Communication Foundation
Professional LINQ
========================
  #6 (permalink)  
Old September 10th, 2009, 05:13 PM
Authorized User
 
Join Date: Sep 2009
Location: Everett, WA
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
Post No auto gen

I typed all the code in manually. I did not use either tool. However I just generated the code using sqlmetal for the entire data base and copied it into my class. It seems to have done a much better job than I did all be it a bit large. Now I am engaged in the arduous labor of replacing all the references to point to the new context. After that I am hoping it will compile and properly identify the key. From the looks of it, it will. Based on the intelesense, the data is much more accesable throughout the application now.
One problem has cropped up however: I have an class.int that was fine before, but now I am getting an error that it can not convert int to int when it analyzes this kind of statement: class.integeritem=item.integerfromLinqDataTable.
I have not looked at it closely yet as I was a bit frustrated to find that I may have to do more than just change the table references.

Last edited by DRCTECH; September 10th, 2009 at 05:15 PM.
  #7 (permalink)  
Old September 11th, 2009, 12:21 AM
Authorized User
 
Join Date: Sep 2009
Location: Everett, WA
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
Default

So, after making a few statements work, though highly inefficient. From
Code:
 class.dectype=class.dectype+dbfield.dectype;
to
Code:
 class.dectype=Convert.ToDecimal(Convert.ToDouble(class.dectype)+Convert.ToDouble(dbfield.dectype));
I took a closer look at the error to find that it was not "cannot convert int to int but rather int? to int. So SQLMetal has saved me the trouble of putting a bunch of zeros in null fields of tables. And a little attention to detail has saved me from spending a lot of time typing some pretty ridiculous code. In addition to that my database is much more accessible throughout the application and I have saved hours of entity definition coding thanks to SQLMetal. And thanks to you for suggesting I regenerate the model.

Last edited by DRCTECH; September 11th, 2009 at 12:23 AM.
 


Thread Tools
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
Foreign key not updating with Primary key xavier1945 BOOK: Access 2003 VBA Programmer's Reference 2 July 4th, 2007 09:48 PM
Primary key cf2006 BOOK: Beginning Visual Basic 2005 Databases ISBN: 978-0-7645-8894-5 1 July 31st, 2006 07:21 PM
FOREIGN KEY and PRIMARY KEY Constraints junemo Oracle 10 June 15th, 2004 01:00 AM



All times are GMT -4. The time now is 05:30 AM.


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