jonpryor / dblinq2007

Automatically exported from code.google.com/p/dblinq2007
0 stars 0 forks source link

DbLinq does not update associations #186

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
DbLinq does not update associations after entities are created (at least
with PostgreSQL). If you query the database after the second
context.SubmitChanges(), you will see the the product's manufacturer Id is
not updated. Here's a little test case (simplified):

CODE:
DataContext context = new MyDataContext (connection);

Product p = new Product();
p.Id = new Random().Next();

Manufacturer m = new Manufacturer();
m.Id = new Random().Next();

context.GetTable<Product>().InsertOnSubmit(p);
context.GetTable<Manufacturer>().InsertOnSubmit(m);

con.SubmitChanges();

p.Manufacturer = m;
con.SubmitChanges();

DATABASE:
CREATE TABLE "Manufacturers" (
    "Id" INTEGER NOT NULL,
    "Name" TEXT,
    PRIMARY KEY ("Id")
);

CREATE TABLE "Products" (
    "Id" INTEGER NOT NULL,
    "Code" TEXT,
    "ManufacturerId" INTEGER REFERENCES "Manufacturers"("Id") ON DELETE SET NULL,
    PRIMARY KEY ("Id")
);

Original issue reported on code.google.com by matthieu...@gmail.com on 29 Jan 2010 at 2:59

GoogleCodeExporter commented 9 years ago
Update: I can make it work if I edit the generated code to use the property
ManufacturerId instead of the field _manufacturerID.

Original comment by matthieu...@gmail.com on 29 Jan 2010 at 3:02

GoogleCodeExporter commented 9 years ago
It does not work, however, if I don't change the generated source code and use 
the
property externally. What the?? :P

Original comment by matthieu...@gmail.com on 31 Jan 2010 at 6:29

GoogleCodeExporter commented 9 years ago
I created a test case against the Northwind database. See Bug #1 in the source 
code.

Original comment by matthieu...@gmail.com on 2 Feb 2010 at 1:11

Attachments:

GoogleCodeExporter commented 9 years ago
I also found this message (May 2009) where Jonathan Pryor talks about this 
issue. It
was supposed to be fixed though :P
http://www.mail-archive.com/dblinq@googlegroups.com/msg00977.html

Original comment by matthieu...@gmail.com on 2 Feb 2010 at 5:27

GoogleCodeExporter commented 9 years ago
This is something different from r1073.

Case in point: when I try to run "Bug 1" under SQL Server, it aborts with:

System.Data.SqlClient.SqlException : Cannot insert explicit value for identity 
column 
in table 'Orders' when IDENTITY_INSERT is set to OFF.

In English, SQL Server doesn't like inserting a row when you're ALSO inserting 
the 
primary key. SQL Server wants to generate the PK, which is counter to your 
example 
code (in which you use 'new Random().Next()' to create one).

If I use this for my Northwind test code:

            var db = CreateDB();

            Order o = new Order();
            o.ShipAddress = "This is really a test.  delete me!";

            Customer c = new Customer();
            c.CustomerID = "TEST!";
            c.CompanyName = "TestCompany";

            db.Orders.InsertOnSubmit(o);
            db.Customers.InsertOnSubmit(c);

            db.SubmitChanges();

            o.Customer = c;
            db.SubmitChanges();

Then the first db.SubmitChanges() call works in SQL Server (and is in the DB).  
The 
second db.SubmitChanges() call fails because, apparently, a SQL command hasn't 
been 
generated (!).

So apparently (1) SQL Server is stricter than PostgreSQL (I'm assuming here 
that 
PostgreSQL isn't generating an error when attempting to execute an empty 
command), 
and (2) nothing is executed with the second db.SubmitChanges() call.

Original comment by jonmpr...@gmail.com on 6 Mar 2010 at 5:01