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.

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:

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:

Any idea why?
You can change the mapping by overwriting your OnModelCreating in your DbContext with this:
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:
and it will map to ProjectId on your Logo entity.