【问题标题】:Two properties related to same table one-to-one与同一张表一对一相关的两个属性
【发布时间】:2016-01-11 13:17:37
【问题描述】:

我想创建具有两个属性的表,这些属性与同一个表具有一对一的关系。我的模型:

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; }
}

这是我的上下文的一部分:

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

它以一对多的方式创建数据库。为什么?

编辑 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; }
}

这是我的上下文的一部分:

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

我有错误:

ProjectImage_Thumbnail_Source: : 多重性在角色中无效 关系中的“ProjectImage_Thumbnail_Source” 'ProjectImage_Thumbnail'。因为从属角色属性是 不是关键属性,多重性的上限 从属角色必须是“*”。

【问题讨论】:

  • 您不能同时让表 A 具有与表 B 相关的主键和外键。将 ImageFile 中的 Guid 属性添加到 ProjectImage 并链接它们。
  • 我编辑了我的问题,现在我有错误

标签: c# sql-server asp.net-mvc entity-framework


【解决方案1】:

试试这个:

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; }
}

【讨论】:

  • 删除可空字符并没有解决我的问题,我仍然有同样的错误
  • @cadi2108 它不仅删除了可为空的,而且还更改了外键,看看
  • 我有错误 'Test' 类型的属性 'FullSizeImage' 上的 ForeignKeyAttribute 无效。在依赖类型“Test”上找不到外键名称“ImageFileGuid”。 Name 值应该是一个逗号分隔的外键属性名称列表。
  • @cadi2108 等等,我忘了问你一个很重要的问题,你用的是什么 ef 版本?
猜你喜欢
  • 2019-10-21
  • 1970-01-01
  • 1970-01-01
  • 2011-10-04
  • 1970-01-01
  • 2015-10-08
  • 2015-12-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多