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 8345565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:43:02+00:00 2026-06-09T06:43:02+00:00

I’m using EF Code First (4.3.1) on a personal ASP.NET MVC 3 project, with

  • 0

I’m using EF Code First (4.3.1) on a personal ASP.NET MVC 3 project, with a very simple domain model, and I’m almost at the point where EF will generate the DB schema the way I want it to.

The domain model has two classes: Painting and Gallery. Each Painting belongs to a single Gallery, and the Gallery has two virtual properties pointing to Painting: One to indicate which of the painting is it’s cover image, and one for which of the paintings is the Slider image displayed on the home page.

The classes are as follow. I’ve removed some annotations and irrelevant properties to make it readable.

public class Gallery
{
    public Gallery()
    {
        Paintings = new List<Painting>();
    }

    [ScaffoldColumn(false)]
    [Key]
    public int GalleryId { get; set; }

    public string Name { get; set; }

    [ScaffoldColumn(false)]
    [Column("LaCover")]
    public Painting Cover { get; set; }

    [ScaffoldColumn(false)]
    [Column("ElSlider")]
    public Painting Slider { get; set; }

    [ScaffoldColumn(false)]
    public virtual List<Painting> Paintings { get; set; }
}

and painting:

public class Painting
{
    [ScaffoldColumn(false)]
    [Key]
    public int PaintingId { get; set; }

    public string Name { get; set; }

    public int GalleryId { get; set; }

    [Column("GalleryId")]
    [ForeignKey("GalleryId")]
    [InverseProperty("Paintings")]
    public virtual Gallery Gallery { get; set; }

    public string Filename { get; set; }
}

It generates a correct db schema for both classes and its relationships, the only small issue I have is that I haven’t found a way to control the column names it gives to the virtual properties of Cover and Slider in the Gallery table.

It’ll name them Cover_PaintingId and Slider_PaintingId.

I tried using the [Column("columnNameHere")] attribute, but that doesn’t affect it at all. As in “I typed a certain non related word and it didnt show up in the schema”.

I’d like to name it CoverPaintingId, without the underscore.

Any help is greatly appreciated. Thanks

  • 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-09T06:43:04+00:00Added an answer on June 9, 2026 at 6:43 am

    The ForeignKey attribute lets you define the property that will act as your foreign key if you want it to exist within your model:

    [ForeignKey("CoverPaintingId")]
    public virtual Painting Cover { get; set; }
    public int? CoverPaintingId { get; set; }
    

    Note you can put the attribute either on the virtual property on the foreign key – just need to specify the name of the “other one”.

    However, since you will have two relationships between the same set of entities, you won’t be able to do this without disabling Cascading deletes on one or both of them. This can only be done using the Fluent Configuration API.

    public class Gallery
    {
        public int GalleryId { get; set; }
    
        [ForeignKey("CoverPaintingId")]
        public virtual Painting Cover { get; set; }
        public int? CoverPaintingId { get; set; }
    
        [ForeignKey("SliderPaintingId")]
        public virtual Painting Slider { get; set; }
        public int? SliderPaintingId { get; set; }
    
    }
    
    public class Painting
    {
        public int PaintingId { get; set; }
    }
    
    public class MyContext : DbContext
    {
        public DbSet<Gallery> Galleries { get; set; }
        public DbSet<Painting> Paintins { get; set; }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Gallery>().HasOptional(g => g.Cover).WithMany().WillCascadeOnDelete(false);
            modelBuilder.Entity<Gallery>().HasOptional(g => g.Slider).WithMany().WillCascadeOnDelete(false);
        }
    }
    

    If you don’t want the foreign key properties to exist in your code model, you can also configure these by using .Map(…) before the .WillCascadeOnDelete(false) part of the API instead of using ForeignKey. I prefer to use Foreign Key, but here’s how the code would look if you wanted to do it this way:

    public class Gallery
    {
        public int GalleryId { get; set; }
        public virtual Painting Cover { get; set; }
        public virtual Painting Slider { get; set; }
    }
    
    public class Painting
    {
        public int PaintingId { get; set; }
    }
    
    public class MyContext : DbContext
    {
        public DbSet<Gallery> Galleries { get; set; }
        public DbSet<Painting> Paintins { get; set; }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Gallery>().HasOptional(g => g.Cover).WithMany().Map(m => m.MapKey("CoverPaintingId")).WillCascadeOnDelete(false);
            modelBuilder.Entity<Gallery>().HasOptional(g => g.Slider).WithMany().Map(m => m.MapKey("SliderPaintingId")).WillCascadeOnDelete(false);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a simple page using Google Maps API 3. My first. One marker
We're building an app, our first using Rails 3, and we're having to build
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am doing a simple coin flipping experiment for class that involves flipping 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.