Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7998997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:22:23+00:00 2026-06-04T15:22:23+00:00

suppose I have the following tables and relations: CREATE TABLE [dbo].[Customers] ( [CustomerID] [int]

  • 0

suppose I have the following tables and relations:

CREATE TABLE [dbo].[Customers] (
    [CustomerID] [int] IDENTITY(1,1) NOT NULL,
    [Description] [nvarchar](255) NOT NULL
 CONSTRAINT [PK_Customers] PRIMARY KEY CLUSTERED ([CustomerID] ASC) 

CREATE TABLE [dbo].[Orders](
    [OrderID] [int] IDENTITY(1,1) NOT NULL,
    [CustomerID] [int] NOT NULL,
        ...
 CONSTRAINT [PK_Orders] PRIMARY KEY CLUSTERED ([OrderID] ASC)

ALTER TABLE [dbo].[Orders]  WITH CHECK ADD  CONSTRAINT [FK_Orders_Customers] 
    FOREIGN KEY([CustomerID]) REFERENCES [dbo].[Customers] ([CustomerID])

I am using some T4 templates to generate simple POCO classes with a property for each database column. This is very easy and really funny. In fact, the code I have is able to generate a single class for each table like in the following sample

public class Customer {
    public int CustomerID { get; set; }
    public string Description { get; set; }
}

I would like now to also create code that maps relationship between tables (1-N, 1-1) so to obtain, for example a class like this one

public class Customer {
    public int CustomerID { get; set; }
    public string Description { get; set; }
    public IList<Order> Orders { get; set; }
}

Unfortunately my code does not work. Here it is:

foreach ( NavigationProperty navProperty in entity.NavigationProperties.Where( p => p.DeclaringType == entity ) ) {
    string elementType = ((EntityType)( (RefType)navProperty.ToEndMember.TypeUsage.EdmType ).ElementType).Name;
    string relationshipName = navProperty.ToEndMember.DeclaringType.FullName;
    string targetRoleName = navProperty.ToEndMember.Name;
    if ( navProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many ) {
        <#=Accessibility.ForProperty( navProperty ) #> IList<<#=navProperty.Name#>> <#=navProperty.Name#> { get; set; }
    }
}

Unfortunately this generate code like this one:

public class Customer {
    public int CustomerID { get; set; }
    public string Description { get; set; }
    public IList<Orders> Orders { get; set; }   // NOTE THE PLURAL IN THE TYPE NAME!!!
}

which is the entity set name not the entity name. What I have to do to solve this problem?

NOTE: I am using EF for the .NET 3.5 version. Also I am including the EF.Utility.CS.ttinclude available in Visual Studio 10.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T15:22:25+00:00Added an answer on June 4, 2026 at 3:22 pm

    Found the solution. This is the correct code:

    foreach ( NavigationProperty navProperty in entity.NavigationProperties.Where( p => p.DeclaringType == entity ) ) {
        string elementType = ((EntityType)( (RefType)navProperty.ToEndMember.TypeUsage.EdmType ).ElementType).Name;
        if ( navProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many ) {
            <#=Accessibility.ForProperty( navProperty ) #> IList<<#= elementType #>> <#=navProperty.Name#> { get; set; }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have the following tables: CREATE TABLE Game ( GameID INT UNSIGNED NOT
I have the following tables; CREATE TABLE IF NOT EXISTS `tags` ( `tag_id` int(11)
Suppose we have 2 tables, a and b: CREATE TABLE a (id_a INT NOT
Suppose I have the following tables CREATE TABLE plugins ( id int primary key,
Suppose that we have following tables create table Employee( 2 EMPNO NUMBER(3), 3 ENAME
Suppose you have the following tables: TABLE Car car_id num TABLE Car_description dsc_id car_id
Suppose you have the following tables: TABLE Car car_id num TABLE Car_description dsc_id car_id
Suppose I have the following two tables in my MySQL database: Table 1:: EMP:
Suppose I have the following objects (one table per object) with this relations: A
Suppose you have to following tables where a sale consists of products and a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.