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

  • Home
  • SEARCH
  • 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 9075973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:59:55+00:00 2026-06-16T18:59:55+00:00

I trying to map my classes to existing database, but failed in part of

  • 0

I trying to map my classes to existing database, but failed in part of foreign keys declaration. Actually, I do not want to have field-foreign key, but only navigation proerty.
Here is models:

public class Template : BaseNamedType
{
    //One template may has multiple TemplateSection
    public virtual ICollection<TemplateSection> TemplateSection { get; set; }
    ...........
}

//Custom mapping class. I have field Order, without it mapping is simple
public class TemplateSection : BaseType
{
    public virtual int Order { get; set; }
    //Here is one-to-many relation, this field is required
    public virtual Template Template { get; set; }
    //Here is one-to-many relation, this field is required
    public virtual Section Section { get; set; }
}

public class Section : BaseNamedType
{
    //One section may be referenced by multiple TemplateSection
    public virtual ICollection<TemplateSection> SectionTemplates { get; set; }
    ...........

}

Here is my database creation script:

CREATE TABLE [templates]
(
    [id]           INT NOT NULL IDENTITY(1, 1),
    [name]         NVARCHAR(300) NOT NULL,
);

GO;

CREATE TABLE [sections_to_templates]
(
    [section_id]  INT NOT NULL,               //FK to [sections]
    [template_id] INT NOT NULL,               //FK to [templates]
    [order]       INT NOT NULL DEFAULT(0)
);

GO

CREATE TABLE [sections]
(
    [id]           INT NOT NULL IDENTITY(1, 1),
    [name]         NVARCHAR(300) NOT NULL,
);

GO

And here in my models binding code, I am absoulte not sure that it is correct:

modelBuilder.Entity<Template>()
    .HasKey(t0 => t0.Id)
    .Map(m => m.ToTable("templates"))
    .Property(x => x.Id)
    .HasColumnName("id")
    .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
    .IsRequired();
modelBuilder.Entity<Template>()
    .HasMany(t0 => t0.TemplateSection)
    .WithRequired(t1 => t1.Template)
    .Map(??????????????????)

modelBuilder.Entity<TemplateSection>()
    .HasKey(t0 => t0.Id)
    .Map(m => m.ToTable("sections_to_templates"))
    .Property(x => x.Id)
    .HasColumnName("id")
    .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
    .IsRequired();
modelBuilder.Entity<TemplateSection>()
    .HasRequired(t0 => t0.Template)
    .WithMany(t1 => t1.TemplateSection)
    .Map(m => m.MapKey("template_id"));
modelBuilder.Entity<TemplateSection>()
    .HasRequired(t0 => t0.Section)
    .WithMany(t1 => t1.SectionTemplates)
    .Map(m => m.MapKey("section_id"));
//How to describe Section class????
  • 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-16T18:59:57+00:00Added an answer on June 16, 2026 at 6:59 pm

    You are almost done in my opinion:

    • Remove the second mapping block, it is redundant because the relationship is already covered in the fourth mapping block.

    • You can remove the IsRequired() mappings because non-nullable properties can only be required.

    • You can remove the HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity) mappings because integer keys are database generated identities by default.

    • For the string properties Name you can add:

      modelBuilder.Entity<Template>()
          .Property(x => x.Name)
          .HasColumnName("name")
          .HasMaxLength(300)
          .IsRequired();
      // the same for Section
      
    • “How to describe Section class????“: Exactly the same way as your first mapping block.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to map an object to a database using SQLAlchemy but have
I have 3 classes that I'm trying to map using fluent nHibernate, but I've
I have two classes where one inherits the other. I'm trying to map my
I'm working with an open source database. I'm trying to map it to classes
I'm trying to map an existing database using Entity Framework Code First Fluent API.
I have two classes that I'm trying to map in Loquacious Nhibernate. The mapping
Trying to map the following schema using the Entity Framework. A Customer can have
I'm trying to map a Dictionary containing Lists. I have the following set of
I am trying to map a legacy database here and I'm running into a
I have an existing DB with a very simple one-way foreign key relationship between

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.