Question.

Posts   
 
    
netLearner
User
Posts: 150
Joined: 18-Oct-2003
# Posted on: 18-Mar-2005 03:50:52   

Frans At Line 5: where i am calling c.Orders.Add(o) It is generating a select, based on where CustomerID = C1. Is there a way i can avoid generating this select becuase this is a new entity i donot want it to go to DB and get my orders for that entity. I just want to add a new Order to the Customer. Please let me know. Thanks.

CustomerEntity c = new CustomerEntity(); c.CustomerID = "C1"; c.CompanyName = "SaiCompany"; OrderEntity o = new OrderEntity(); /* Question in this line*/ c.Orders.Add(o); // Generates a select c.Save(true);

netLearner
User
Posts: 150
Joined: 18-Oct-2003
# Posted on: 18-Mar-2005 03:52:17   

Also i am wondering why we do not have a set property for Orders. Thanks.

netLearner
User
Posts: 150
Joined: 18-Oct-2003
# Posted on: 18-Mar-2005 05:04:01   

I think i figured it. Instead of: c.Orders.Add(o); o.Save(true);

do: o.Customer = c; o.Save(true);

This will avoid an unncessary additional select. Thanks.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 18-Mar-2005 10:01:40   

Yes, the c.Orders. triggers a lazy load of all orders. Your 3rd message explains the right way to do it simple_smile

Frans Bouma | Lead developer LLBLGen Pro
netLearner
User
Posts: 150
Joined: 18-Oct-2003
# Posted on: 18-Mar-2005 19:35:15   

I have 2 tables User (PK -> UserID -> Identity) and UserRole (PK -> UserRoleID -> Identity)

Inorder to create a new user and userrole this is what i am doing:

UserEntity user = new UserEntity(); UserRoleEntity ur = new UserRoleEntity();

user.UserName = "Test"; ur.UserId = user.UserId; ur.RoleId = 1; ur.User = user;

Now to save recursively user.Save(true);

I get this exception:

  • {"An exception was caught during the execution of an action query: Line 1: Incorrect syntax near ')'.. Check InnerException, QueryExecuted and Parameters of this exception to examine the cause of this exception." } System.Exception

Here is the profiler info:

declare @P1 int set @P1=NULL exec sp_executesql N'INSERT INTO [dbo].[Users] () VALUES ();SELECT @UserId=SCOPE_IDENTITY()', N'@UserId int output', @UserId = @P1 output select @P1

Obviously p1 cannot be null. Does this mean before saving User i need to first find out an id which does not exist in User table and then set the Id first followed by saving it or if there is an alternative approach for this. Please let me know.

netLearner
User
Posts: 150
Joined: 18-Oct-2003
# Posted on: 18-Mar-2005 23:50:14   

Hi Frans, Please ignore the above message. Thanks.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 19-Mar-2005 09:44:37   

netLearner wrote:

Hi Frans, Please ignore the above message. Thanks.

Heh ok simple_smile Glad it's solved simple_smile

Frans Bouma | Lead developer LLBLGen Pro