1

I want to create table with two properties with relations to same table, one-to-one. My model:

public class ImageFile : BaseEntity
{
    [Key]
    Guid Guid { get; set; }
    public string Name { get; set; }        
    public string Extension { get; set; }

    public long Size { get; set; }
    public byte[] FileContent { get; set; }                
    public ProjectImage ProjectImage { get; set; }
}

public class ProjectImage : BaseEntity
{    
    public string Description { get; set }            
    public ImageFile Thumbnail { get; set; }
    public ImageFile FullSizeImage { get; set; }
}

Here is the part of my context:

modelBuilder.Entity<ProjectImage>()
    .HasRequired(f => f.FullSizeImage)
    .WithOptional(i => i.ProjectImage);
modelBuilder.Entity<ProjectImage>()
    .HasRequired(f => f.Thumbnail)
    .WithOptional(i => i.ProjectImage);

It create database one-to-many. Why?

EDIT V2:

public class ImageFile : BaseEntity
{
    [Key]
    Guid Guid { get; set; }
    public string Name { get; set; }        
    public string Extension { get; set; }

    public long Size { get; set; }
    public byte[] FileContent { get; set; }    
    public Guid? ProjectImageGuid { get; set; }

    [ForeignKey("ProjectImageGuid")]            
    public ProjectImage ProjectImage { get; set; }
}

public class ProjectImage : BaseEntity
{    
    public string Description { get; set }            
    public Guid? ThumbnailGuid { get; set; }        
    public Guid? FullSizeImageGuid { get; set; }

    [ForeignKey("ThumbnailGuid")]
    public ImageFile Thumbnail { get; set; }

    [ForeignKey("FullSizeImageGuid")]
    public ImageFile FullSizeImage { get; set; }
}

Here is the part of my context:

modelBuilder.Entity<ProjectImage>()
    .HasRequired(f => f.FullSizeImage)
    .WithOptional(i => i.ProjectImage);
modelBuilder.Entity<ProjectImage>()
    .HasRequired(f => f.Thumbnail)
    .WithOptional(i => i.ProjectImage);

I have error:

ProjectImage_Thumbnail_Source: : Multiplicity is not valid in Role 'ProjectImage_Thumbnail_Source' in relationship 'ProjectImage_Thumbnail'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.

ocuenca
  • 38,548
  • 11
  • 89
  • 102
cadi2108
  • 1,280
  • 6
  • 19
  • 43
  • You can't have table A with a primary key and a foreign key relating to table B at the same time. Add the Guid property from ImageFile to ProjectImage and link them. – Kami Jan 11 '16 at 13:27
  • I edited my question, now I have error – cadi2108 Jan 11 '16 at 13:43
  • 1
    http://stackoverflow.com/questions/18724964/entity-framework-1-to-1-relationship-using-code-first-how check this – Zergling Jan 11 '16 at 13:55

1 Answers1

0

Try this:

public class ImageFile : BaseEntity
{
    [Key]
    Guid Guid { get; set; }
    public string Name { get; set; }        
    public string Extension { get; set; }

    public long Size { get; set; }
    public byte[] FileContent { get; set; }    


    public Guid ProjectImageGuid { get; set; }

    [ForeignKey("ProjectImageGuid")]            
    public ProjectImage ProjectImage { get; set; }
}

public class ProjectImage : BaseEntity
{    
    public string Description { get; set }            
    public Guid ThumbnailGuid { get; set; }        
    public Guid FullSizeImageGuid { get; set; }

    [ForeignKey("ImageFileGuid")]
    public ImageFile Thumbnail { get; set; }

    [ForeignKey("ImageFileGuid")]
    public ImageFile FullSizeImage { get; set; }
}
Felix Av
  • 1,254
  • 1
  • 14
  • 22
  • Removing of nullable character didn't resolve my problem, I have still same error – cadi2108 Jan 11 '16 at 14:34
  • @cadi2108 its not just removing the nullable but also foreign key changed, take a look – Felix Av Jan 11 '16 at 14:40
  • I have error The ForeignKeyAttribute on property 'FullSizeImage' on type 'Test' is not valid. The foreign key name 'ImageFileGuid' was not found on the dependent type 'Test'. The Name value should be a comma separated list of foreign key property names. – cadi2108 Jan 11 '16 at 15:00
  • @cadi2108 wait, I forgot to ask you very important question, what ef version you're using? – Felix Av Jan 11 '16 at 15:08