New Posts New Posts RSS Feed: First time trying to use Generate Id
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

First time trying to use Generate Id

 Post Reply Post Reply
Author
ting View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 27-Mar-2009
Location: San Francisco
Posts: 427
Post Options Post Options   Quote ting Quote  Post ReplyReply Direct Link To This Post Topic: First time trying to use Generate Id
    Posted: 05-Aug-2010 at 1:25pm

Glad you got it working!  Thanks for letting us know.

Back to Top
BillG View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 05-Dec-2007
Location: Monroe, MI
Posts: 233
Post Options Post Options   Quote BillG Quote  Post ReplyReply Direct Link To This Post Posted: 04-Aug-2010 at 3:26pm
I finally got it to work. I made two mistakes. I didn't change the namespace in the NumericGenerator and I didn't change the datasourcekey(). I did have to change the code to look for a field called nextid1 because the designer would not allow me to have a fieldname with the same name as the table.


Back to Top
ting View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 27-Mar-2009
Location: San Francisco
Posts: 427
Post Options Post Options   Quote ting Quote  Post ReplyReply Direct Link To This Post Posted: 04-Aug-2010 at 3:09pm
So a few comments:
 
1)  When you are using invoiceno as the primary key, are you using our NumericIdGenerator or did you implement your own IIDGenerator?  (Or if you have none, that might also explain the null insert exception.)
 
2)  If you use Identity columns for the primary keys, you don't need to implement an IIdGenerator or call EntityManager.GenerateId().
 
3)  For the SalesInvoice table, it may be simpler to declare the invoiceno as an Identity column, but if this invoiceno is visible to the end user, you will buy some future flexibility by having a separate column as the primary key.  It's a little more work though as you still have to create a unique invoiceno for each SalesInvoice.
 
If you want to keep things simple, just declare everything as an Identity column and then you don't have to worry about any ID Generation.  Using GUIDs as primary keys is also simple and has the advantage that the entities don't have to use a temporary ID before being assigned one from the server.  Their disadvantage is of course that they are long and hard to read.
 
Back to Top
BillG View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 05-Dec-2007
Location: Monroe, MI
Posts: 233
Post Options Post Options   Quote BillG Quote  Post ReplyReply Direct Link To This Post Posted: 03-Aug-2010 at 5:28pm
Are you suggesting this?
 
SalesInvoice table
 
Id int identity
int invoiceno   generated by nextinvioceno from a lookup table
 
 
SalesInvoiceLine table
id in identity
int invoiceno
int lineno
 
 
 
 
Back to Top
BillG View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 05-Dec-2007
Location: Monroe, MI
Posts: 233
Post Options Post Options   Quote BillG Quote  Post ReplyReply Direct Link To This Post Posted: 03-Aug-2010 at 5:26pm
This is actually the error I am getting
 
Cannot insert the value NULL into column 'InvoiceNo', table 'SEIU99.dbo.SalesInvoices'; column does not allow nulls. INSERT fails.
The statement has been terminated.
 
 
Salesinvoices is the parent table. Right before it goes into the SaveChanges method SalesInvoice.InvoiceNo = -100
and the lone detail record is -100,1
Back to Top
ting View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 27-Mar-2009
Location: San Francisco
Posts: 427
Post Options Post Options   Quote ting Quote  Post ReplyReply Direct Link To This Post Posted: 03-Aug-2010 at 5:08pm

Can you confirm that each SalesInvoiceLine is pointing to the correct SalesInvoice?

What is the "invoices" property on SalesInvoice that the exception is complaining should not be null?

Also, if you have flexibility on the schema, consider creating a dedicated primary key with no semantic information on your tables instead of using a compound key.  Performance will be better, and if the application grows, it supports a larger variety of scenarios.

Back to Top
BillG View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 05-Dec-2007
Location: Monroe, MI
Posts: 233
Post Options Post Options   Quote BillG Quote  Post ReplyReply Direct Link To This Post Posted: 03-Aug-2010 at 9:53am
Any ideas?
Back to Top
BillG View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 05-Dec-2007
Location: Monroe, MI
Posts: 233
Post Options Post Options   Quote BillG Quote  Post ReplyReply Direct Link To This Post Posted: 01-Aug-2010 at 2:04pm
I have two tables in my database SalesInvoices and SalesInvoiceLines. The SalesInvoices table has a pk of invoiceNo
and the SalesInvoicesLine table has a pk of InvoiceNo and LineNo. The SalesInvoiceLines table has a FK to invoiceno of SalesInvoice. I have a table named NextId with a field called Global and it has a value of 100 in it.
 
Here is my SalesInvoice.Create
 

public static SalesInvoice Create(IbEm.EntityManager mgr)

{

SalesInvoice aSalesInvoice = mgr.CreateEntity<SalesInvoice>();

aSalesInvoice.InvoiceDate = DateTime.Now;

aSalesInvoice.InvoiceTotal = 0;

aSalesInvoice.AmountPaid = 0;

mgr.GenerateId(aSalesInvoice, SalesInvoice.PropertyMetadata.InvoiceNo);

aSalesInvoice.EntityAspect.AddToManager();

return aSalesInvoice;

}

 
Here is my save. I am saving the currentSalesInvoice which is an object of the SalesInvoice class and the detail lines which are a collection of the SalesInvoiceLines class.
 

try

{

List<Entity> changedEntities = new List<Entity>();

changedEntities.Add(currentSalesInvoice);

foreach (SalesInvoiceLine s in invoiceLines)

{

changedEntities.Add(s);

}

entityManager.SaveChanges(changedEntities);

invoiceLines = null;

}

catch(Exception ex)

{

throw new Exception(ex.Message);

}

when the save occurs I am getting a cannot insert into salesinvoices table with a null value for invoices.
 
 
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down