codesmithtools / Templates

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

Many to Many Did Errored on DataAccess.csp #707

Closed JustinBourlier closed 8 years ago

JustinBourlier commented 8 years ago

I have a simple table with 2 foreign keys, I received this error: Entity does not contain a key.

niemyjski commented 8 years ago

What set of templates is this for? Would you mind posting a sample schema that reproduces this issue and I can give you more info?

JustinBourlier commented 8 years ago

This is on the DataAccess.csp file. Here is the schema / database script of just 3 tables. All I get is the BookAuthorFactory.cs, seems like it ran the very first time, but if I deleted BookAuthorFactory.cs, and re-ran it I got an error. I set IsolationLevel to ReadComitted, AutoExecStoredProcedures is checked, DataAccess is ObjectFactoryStoredProcedures

C:\CodeSmith Generator\Templates\TD\TD.Data\DataAccess.csp(0,0) Entity does not contain a key. Stack Trace: at _CodeSmith.DataAccessLayer_cst.SqlStoredProcedures.RenderMethod1(TextWriter writer, Control control) at CodeSmith.Engine.DelegateControl.Render(TextWriter writer) at CodeSmith.Engine.Control.RenderChildren(TextWriter writer) at CodeSmith.Engine.Control.Render(TextWriter writer) at CodeSmith.Engine.CodeTemplate.Render(TextWriter writer) at CodeSmith.Engine.CodeTemplate.RenderToString() at CodeSmith.Engine.CodeTemplate.RenderToFile(OutputFile outputFile, Boolean overwrite) at CodeSmith.Engine.CodeTemplate.RenderToFile(String path, String dependentUpon, Boolean overwrite) at _CodeSmith.DataAccessLayer_cst.RenderObjectFactoryTemplates(IEntity entity, Boolean IsReadOnly, Boolean IsChild) at _CodeSmith.DataAccessLayer_cst.Generate() at _CodeSmith.DataAccessLayer_cst.RenderMethod1(TextWriter writer, Control control) at CodeSmith.Engine.DelegateControl.Render(TextWriter writer) at CodeSmith.Engine.Control.RenderChildren(TextWriter writer) at CodeSmith.Engine.Control.Render(TextWriter writer) at CodeSmith.Engine.CodeTemplate.Render(TextWriter writer) at CodeSmith.Engine.CodeTemplate.RenderToString() at CodeSmith.Engine.ProjectCodeGenerator.GenerateCode(CodeSmithProject project)

Unable to render output DataAccessLayer.cs, skipping...

USE [TestDatabase] GO /\ Object: Table [dbo].[Author] Script Date: 8/16/2016 11:51:36 AM **/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[Author]([Author_ID] [int] NOT NULL, [Name] [varchar]%2850%29 NOT NULL, CONSTRAINT [PK_Author] PRIMARY KEY CLUSTERED %28 [Author_ID] ASC %29WITH %28PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON%29 ON [PRIMARY]) ON [PRIMARY]

GO SET ANSI_PADDING OFF GO /\ Object: Table [dbo].[Book] Script Date: 8/16/2016 11:51:36 AM **/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[Book]([Book_ID] [int] NOT NULL, [Name] [varchar]%2850%29 NULL, CONSTRAINT [PK_Book] PRIMARY KEY CLUSTERED %28 [Book_ID] ASC %29WITH %28PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON%29 ON [PRIMARY]) ON [PRIMARY]

GO SET ANSI_PADDING OFF GO /\ Object: Table [dbo].[BookAuthor] Script Date: 8/16/2016 11:51:36 AM **/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[BookAuthor]([Book_ID] [int] NOT NULL, [Author_ID] [int] NOT NULL) ON [PRIMARY]

GO ALTER TABLE [dbo].[BookAuthor] WITH CHECK ADD CONSTRAINT [FK_BookAuthor_Author] FOREIGN KEY([Author_ID]) REFERENCES [dbo].Author GO ALTER TABLE [dbo].[BookAuthor] CHECK CONSTRAINT [FK_BookAuthor_Author] GO ALTER TABLE [dbo].[BookAuthor] WITH CHECK ADD CONSTRAINT [FK_BookAuthor_Book] FOREIGN KEY([Book_ID]) REFERENCES [dbo].Book GO ALTER TABLE [dbo].[BookAuthor] CHECK CONSTRAINT [FK_BookAuthor_Book] GO

JustinBourlier commented 8 years ago

FYI, I noticed that if I make it a composite primary key on the BookAuthor table, then it generates appropriately. Which is probably what I should have done in the first place.

niemyjski commented 8 years ago

Thanks for the heads up. There are rules that are checked when dealing with many-to-manies in most of the templates.