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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:21:57+00:00 2026-06-10T02:21:57+00:00

I use Entity Framework with ASP.NET MVC3 Code First. I have a project entity

  • 0

I use Entity Framework with ASP.NET MVC3 Code First.

I have a project entity and logo like this:

public class Project
{
    [Key]
    public int      ProjectID   { get; set; }
    public string   Name        { get; set; }
    public string   Description { get; set; }        

    public virtual  Logo Logo   { get; set; }
}

public class Logo
{
    [Key]
    public int    LogoID          { get; set; }
    public string LogoName        { get; set; }
    public byte[] LogoContent     { get; set; }
    public string LogoContentType { get; set; }
}

Each project can have 0 or 1 logo attached.

When my database (sql server) is created, I see that a field is created in the table Project for referencing the logo.

enter image description here

Whenever I have a project in my Project table referencing a logo,I have the Logo_LogoID filled with the corresponding key in the logo table. That’s ok for me. What seems strange to me is when I delete the logo, sql server show me the error below:

enter image description here

So in order to be able to delete a logo from my Logo table, I need first to break the reference from the Project table (by replacing the LogoID with NULL) and next delete the row from Logo.

Maybe there is a better way of referencing things?

This may seems a stupid question.

I think a better solution would be to have the inverse relation: the ProjectID field added in the Logo table. So whenever I need to delete a logo, I can easily delete the Logo.

What do you think?

Thanks.

EDIT

Like Mark Oreta suggest, I tried with:

        modelBuilder.Entity<Project>()
            .HasOptional(p => p.Logo)
            .WithOptionalPrincipal(); 

But I got a double reference to ProjectID:

enter image description here

Any idea why?

  • 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-10T02:21:59+00:00Added an answer on June 10, 2026 at 2:21 am

    You can change the mapping by overwriting your OnModelCreating in your DbContext with this:

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Project>()
                .HasOptional(p => p.Logo)
                .WithOptionalPrincipal();
        }
    

    This will create a Project_ProjectID on your Logo entity.

    If you want to customize the name – you said you were ok with it, but you might want to change it later, you can add this to the end:

    .Map(m => m.MapKey("ProjectId")
    

    and it will map to ProjectId on your Logo entity.

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

Sidebar

Related Questions

I have an ASP.NET MVC3 application, where I use Entity Framework 4.3 Code-First and
Assume we have an ASP.NET MVC3 Web site that makes use of Entity Framework
I have an ASP.NET MVC 3 application using an Entity Framework (4.3.1) Code First
I am using Entity Framework 4.1 code first and ASP.NET MVC 3. I have
In ASP.NET MVC3 projects, is it faster, more secure, to use Entity Framework (with
I'm using ASP.NET MVC 3 with the Entity Framework 4 code first approach and
I'm using Entity Framework in an ASP.NET MVC3 application and I'm trying to use
For my ASP.NET MVC 2 application I use Entity Framework 1.0 as my data
How do I use Datatable instead of Entity framework in Asp.Net mvc? I'm referring
I use Entity Framework code-first. DataBaseName - I want to create a database in

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.