codesmithtools / Templates

CodeSmith Generator Templates
http://www.codesmithtools.com/product/generator
54 stars 35 forks source link

Problems with a table has both a foreign and primary key on same column #709

Closed JustinBourlier closed 4 years ago

JustinBourlier commented 7 years ago

Like the subject says, I have 2 tables, that form a 1-1 relationship. They both have the same primary key, the second table also has a FK link to the first. I'm not getting the methods generated to handle this scenario. Tried making them both root, 1 root, 1 child, no dice.

niemyjski commented 7 years ago

What set of templates is this for?

JustinBourlier commented 7 years ago

CSLA Templates, utilizing "StoredProcedures" Data Access Implementation. I'll try and produce a smaller test case, because I have some other tables that work fine with this setup. Problem I'm seeing is say I have table A, table B. both have same primary key, B has a foreign key to A. There is no Chind_Insert(Entity A...., or Child_Update(Entity A....) functions being generated.

JustinBourlier commented 7 years ago

Okay, I whipped up a simple test, and I get this error:

{"Unable to cast object of type 'Business.A' to type 'System.Data.SqlClient.SqlConnection'."}

I have table A as an editable root Table B is a child C# Code:

            A test = A.NewA();
            test.Name = "Justin";

            test.B.Gender = "Male";
            test.Save();

SQL Script:

CREATE TABLE [dbo].[A]( [First_ID] [int] IDENTITY(1,1) NOT NULL, [Name] varchar NULL, CONSTRAINT [PK_A] PRIMARY KEY CLUSTERED ( [First_ID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO

CREATE TABLE [dbo].[B]( [Second_ID] [int] NOT NULL, [Gender] varchar NULL, CONSTRAINT [PK_B] PRIMARY KEY CLUSTERED ( [Second_ID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY]

GO ALTER TABLE [dbo].[B] WITH CHECK ADD CONSTRAINT [FK_B_A] FOREIGN KEY([Second_ID]) REFERENCES [dbo].[A] ([First_ID]) GO ALTER TABLE [dbo].[B] CHECK CONSTRAINT [FK_B_A] GO

JustinBourlier commented 7 years ago

I also attempted to make them both editable roots, but the identity column doesn't pop out for "A" when from the calling location. It's a bit odd because it is being set in DataAccess.cs file. I just get a 0 back every time.

            A test = A.NewA();
            test.Name = "Justin";
            test.Save();

            if (!B.Exists(new BCriteria { SecondID = test.FirstID}))
            {
                B testB = B.NewB();
                testB.SecondID = test.FirstID;
                testB.Gender = "Male";
                testB.Save();
            }
            else
            {
                test.B.Gender = "Male";
                test.B.Save();
            }
JustinBourlier commented 7 years ago

Just a small update to you. So I feel a bit silly, I didn't realize that the object changed, once I did test = test.Save(), then I get my identity column, and this works just fine when they are both editable roots. However, if I make table A a root, and table B a child, it doesn't work, as there is no Child_Insert(A, or Child_Update(A in the B entity to utilize for upserting the primary key it would get from parent A.

niemyjski commented 7 years ago

You may have to make one a switchable object. I can't remember the specifics as it's been quite some time since I had a production csla app.. They are both roots but also have a relationship together, it kind of makes sense, please correct me if I'm wrong.

JustinBourlier commented 7 years ago

I had a look at switchable object, from what I can tell it looks like if you basically generated a root and a child, and combined those functions into one file. It still is missing the DataPortal/Child_Insert(Parent entity

So I get an invalid cast when it tries to call updatechildren. I understand if it being so long, you put together a good place to start, something that either can be built on, or maybe I'm still not quite understanding CSLA (which is possible!)

niemyjski commented 7 years ago

A switchable object should have both parent and children operations. Is there any chance you could debug this and figure out why the child_insert isn't being generated. From the looks of it we generate based on association types: https://github.com/codesmithtools/Templates/blob/ea981404d0659899bf786f3dfb787e7467484e39/Templates/Frameworks/Csla/CSharp/BusinessLayer/Internal/EditableChild.DataAccess.ParameterizedSQL.cst#L64

There is a template in the Common folder (https://github.com/codesmithtools/Templates/blob/ea981404d0659899bf786f3dfb787e7467484e39/Templates/Frameworks/Csla/Common/SchemaHelper.cst) Can you run this file and see what it says the associations are for this table in question.

stale[bot] commented 4 years ago

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. This issue now has been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] commented 4 years ago

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues. This issue now has been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.