in

CodeSmith Community

Your Code. Your Way. Faster!

Many to many generated declaration issue

Last post 05-17-2007 7:55 AM by mike123. 3 replies.
Page 1 of 1 (4 items)
Sort Posts: Previous Next
  • 05-10-2007 7:27 AM

    • millerwa
    • Top 500 Contributor
    • Joined on 05-03-2007
    • Posts 18
    • Points 465

    Many to many generated declaration issue

    The code generated for the many to many relationship I think is getting confused when two Entity Tables both have the same column name for the primary key.
     
    Table structure.
     
    Dealer
    ---------
    PKID int
    Name varchar(100)
    Address1 varchar(150)
    ....
     
    Country
    ----------
    PKID int
    Name varchar(150)
    Code char(4)
     
    DealerCountry
    -----------------
    DealerID
    CountryID
     
    This table has foriegn keys
    DealerID --> Dealer.PKID
    CountryID --> Country.PKID
     
    Now when wanting to edit a Dealer, I should be able to also assign countries to that dealer. However the code generated does is not properly setting some of the attributes properly.
    The issue is that when on the selecting a dealer the querystring has something like .../Admin/DealerEdit.aspx?PKID=174690 which brings up the dealer, but the fails to propely show the related countries associated with the dealer.
    If a country is selected the error of something like "Cannot add PKID to dictionary as it already exists"
     
    So looking at the code generated there are some items that are not correctly set.
     
    Once fixed the page works fine.
     
    Original code generated:
     
    <asp:CheckBoxList ID="CountryList" runat="server" DataSourceID="CountryDataSource" DataTextField="Name" DataValueField="PKID" RepeatColumns="4" />
    <data:CountryDataSource ID="CountryDataSource" runat="server" SelectMethod="GetAll" />
    <data:DealerCountryDataSource ID="DealerCountryDataSource" runat="server" SelectMethod="GetByCountryID">
    <Parameters>
    <asp:QueryStringParameter Name="CountryID" QueryStringField="PKID" Type="String" />
    </Parameters>
    </data:DealerCountryDataSource>
     
    <data:ManyToManyListRelationship ID="DealerCountryRelationship" runat="server">
    <PrimaryMember runat="server" DataSourceID="DealerDataSource" EntityKeyName="PKID" />
    <LinkMember runat="server" DataSourceID="DealerCountryDataSource" EntityKeyName="CountryID" ForeignKeyName="CountryID" />
    <ReferenceMember runat="server" DataSourceID="CountryDataSource" ListControlID="CountryList" EntityKeyName="PKID"/>
    </data:ManyToManyListRelationship>
     
    It should be:
     
    <asp:CheckBoxList ID="CountryList" runat="server" DataSourceID="CountryDataSource" DataTextField="Name" DataValueField="PKID" RepeatColumns="4"/>
    <data:CountryDataSource ID="CountryDataSource" runat="server" SelectMethod="GetAll" />
    <data:DealerCountryDataSource ID="DealerCountryDataSource" runat="server" SelectMethod="GetByDealerID">
    <Parameters>
    <asp:QueryStringParameter Name="DealerID" QueryStringField="PKID" Type="String" />
    </Parameters>
    </data:DealerCountryDataSource>
    <data:ManyToManyListRelationship ID="DealerCountryRelationship" runat="server">
    <PrimaryMember ID="PrimaryMember1" runat="server" DataSourceID="DealerDataSource" EntityKeyName="PKID" />
    <LinkMember ID="LinkMember1" runat="server" DataSourceID="DealerCountryDataSource" EntityKeyName="DealerID" ForeignKeyName="CountryID" />
    <ReferenceMember ID="ReferenceMember1" runat="server" DataSourceID="CountryDataSource" ListControlID="CountryList" EntityKeyName="PKID"/>
    </data:ManyToManyListRelationship>
     
    Is this a bug, or is there something else I missed.
    I tested this further and if the tables do not have use the same field name the code generates correctly.
    • Post Points: 35
  • 05-10-2007 5:22 PM In reply to

    • mike123
    • Top 10 Contributor
    • Joined on 02-25-2005
    • Toronto, Ontario
    • Posts 723
    • Points 16,775

    Re: Many to many generated declaration issue

    Can you post the script of those tables along with relations and keys?
    Mike Shatny
    -------------------------------------
    Member of the .netTiers team
    http://www.nettiers.com
    -------------------------------------
    • Post Points: 35
  • 05-11-2007 10:11 AM In reply to

    • millerwa
    • Top 500 Contributor
    • Joined on 05-03-2007
    • Posts 18
    • Points 465

    Re: Many to many generated declaration issue

    Here you go... this doesn't include all the db tables just the relavent ones

     if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FK_DealerCountry_Country]') and OBJECTPROPERTY(id, N'IsForeignKey') = 1)
    ALTER TABLE [dbo].[DealerCountry] DROP CONSTRAINT FK_DealerCountry_Country
    GO

    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FK_DealerCountry_Dealer]') and OBJECTPROPERTY(id, N'IsForeignKey') = 1)
    ALTER TABLE [dbo].[DealerCountry] DROP CONSTRAINT FK_DealerCountry_Dealer
    GO

    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FK_DealerProximity_Dealer]') and OBJECTPROPERTY(id, N'IsForeignKey') = 1)
    ALTER TABLE [dbo].[DealerProximity] DROP CONSTRAINT FK_DealerProximity_Dealer
    GO

    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FK_DealerAttribute_Dealer]') and OBJECTPROPERTY(id, N'IsForeignKey') = 1)
    ALTER TABLE [dbo].[DealerStoreAttribute] DROP CONSTRAINT FK_DealerAttribute_Dealer
    GO

    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Country]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    drop table [dbo].[Country]
    GO

    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Dealer]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    drop table [dbo].[Dealer]
    GO

    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DealerCountry]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    drop table [dbo].[DealerCountry]
    GO

    CREATE TABLE [dbo].[Country] (
     [PKID] [int] IDENTITY (1, 1) NOT NULL ,
     [Name] [nvarchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
     [Code] [char] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
    ) ON [PRIMARY]
    GO

    CREATE TABLE [dbo].[Dealer] (
     [PKID] [int] IDENTITY (1, 1) NOT NULL ,
     [StoreID] [varchar] (4) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
     [Name] [nvarchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
     [CountryID] [int] NULL ,
     [Street1] [nvarchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
     [Street2] [nvarchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
     [City] [nvarchar] (150) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
     [Region] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
     [RegionID] [int] NULL ,
     [PostalCode] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
     [Phone] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
     [Fax] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
     [URL] [nvarchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
     [Email] [nvarchar] (150) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
     [SalesRep] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
     [ARNumber] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
     [Latitude] [float] NULL ,
     [Longitude] [float] NULL ,
     [StatusCode] [bit] NOT NULL ,
     [UTCLastUpdatedDate] [datetime] NULL ,
     [UpdatedBy] [int] NULL ,
     [UTCCreatedDate] [datetime] NULL ,
     [CreatedBy] [int] NULL
    ) ON [PRIMARY]
    GO

    CREATE TABLE [dbo].[DealerCountry] (
     [DealerID] [int] NOT NULL ,
     [CountryID] [int] NOT NULL
    ) ON [PRIMARY]
    GO

    ALTER TABLE [dbo].[Country] WITH NOCHECK ADD
     CONSTRAINT [PK_Country] PRIMARY KEY  CLUSTERED
     (
      [PKID]
     )  ON [PRIMARY]
    GO

    ALTER TABLE [dbo].[Dealer] WITH NOCHECK ADD
     CONSTRAINT [PK_Dealer] PRIMARY KEY  CLUSTERED
     (
      [PKID]
     )  ON [PRIMARY]
    GO

    ALTER TABLE [dbo].[DealerCountry] WITH NOCHECK ADD
     CONSTRAINT [PK_DealerCountry] PRIMARY KEY  CLUSTERED
     (
      [DealerID],
      [CountryID]
     )  ON [PRIMARY]
    GO

    ALTER TABLE [dbo].[Dealer] ADD
     CONSTRAINT [DF_Dealer_IsActive] DEFAULT (1) FOR [StatusCode],
     CONSTRAINT [DF_Dealer_Created] DEFAULT (getdate()) FOR [UTCCreatedDate]
    GO

     CREATE  INDEX [IX_STORE] ON [dbo].[Dealer]([StoreID]) ON [PRIMARY]
    GO

     CREATE  INDEX [IX_PKID_STOREID_NAME] ON [dbo].[Dealer]([PKID], [StoreID], [Name]) ON [PRIMARY]
    GO

     CREATE  INDEX [IX_DealerName] ON [dbo].[Dealer]([Name]) ON [PRIMARY]
    GO

     CREATE  INDEX [IX_DealerCity] ON [dbo].[Dealer]([City]) ON [PRIMARY]
    GO

     CREATE  INDEX [IX_DealerRegion] ON [dbo].[Dealer]([Region]) ON [PRIMARY]
    GO

     CREATE  INDEX [IX_DealerCountry] ON [dbo].[DealerCountry]([DealerID]) ON [PRIMARY]
    GO

    ALTER TABLE [dbo].[DealerCountry] ADD
     CONSTRAINT [FK_DealerCountry_Country] FOREIGN KEY
     (
      [CountryID]
     ) REFERENCES [dbo].[Country] (
      [PKID]
     ),
     CONSTRAINT [FK_DealerCountry_Dealer] FOREIGN KEY
     (
      [DealerID]
     ) REFERENCES [dbo].[Dealer] (
      [PKID]
     )
    GO

     

    • Post Points: 35
  • 05-17-2007 7:55 AM In reply to

    • mike123
    • Top 10 Contributor
    • Joined on 02-25-2005
    • Toronto, Ontario
    • Posts 723
    • Points 16,775

    Re: Many to many generated declaration issue

    millerwa,

    Fixed in rev. 563. Thanks for the report and accommodated script it was very helpful.

     

    Mike Shatny
    -------------------------------------
    Member of the .netTiers team
    http://www.nettiers.com
    -------------------------------------
    • Post Points: 5
Page 1 of 1 (4 items)
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems