C# 实体框架一对多和一对一关系

C# 实体框架一对多和一对一关系,c#,entity-framework,C#,Entity Framework,我目前有一个数据模型,其中一个属性可以有多个属性图像: public class Property { public int ID { get; set; } public string Name { get; set; } public Guid PrimaryImageID { get; set; } public ICollection<PropertyImage> Images { get; set; } } public class Pr

我目前有一个数据模型,其中一个属性可以有多个属性图像:

public class Property
{
    public int ID { get; set; }
    public string Name { get; set; }
    public Guid PrimaryImageID { get; set; }

    public ICollection<PropertyImage> Images { get; set; }
}

public class PropertyImage
{
    [Key]
    public Guid ID { get; set; }
    public int PropertyID { get; set; }

    public virtual Property Property { get; set; }
}
比如说。如果用户想要更改属性的初始映像,那么我只需将PrimaryImageId字段更改为其他映像的Guid


非常感谢

就我个人而言,我不会为了这个而和EF混在一起,你分享的链接中的答案会非常同意我。我只想向PropertyImage类添加另一个字段

public bool IsPrimaryImage {get;set;}
然后根据其中设置的值查找图像


有时候最简单的解决方案就是最好的。你可能会在EF中得到一个复杂的解决方案,它可以满足你的需要,但最终,它真的会比只给字段赋值true或false好吗?

就我个人而言,我不会和EF混在一起去做这件事,你分享的链接中的答案会非常同意我。我只想向PropertyImage类添加另一个字段

public bool IsPrimaryImage {get;set;}
然后根据其中设置的值查找图像

有时候最简单的解决方案就是最好的。您可能会在EF中得到一个复杂的解决方案,它可以满足您的需要,但在一天结束时,它是否真的比只为字段指定true或false更好?

尝试以下方法:

public class Property
{
  public int ID { get; set; }
  public string Name { get; set; }
  public Guid PrimaryImageID { get; set; }
  [ForeignKey("PrimaryImageID")]
  public virtual PropertyImage PrimaryImage { get; set; }

  public ICollection<PropertyImage> Images { get; set; }
}

public class PropertyImage
{
  [Key]
  public Guid ID { get; set; }
  public int PropertyID { get; set; }

  [ForeignKey("PropertyID")]
  public virtual Property Property { get; set; }
}
公共类属性
{
公共int ID{get;set;}
公共字符串名称{get;set;}
公共Guid PrimaryImageID{get;set;}
[ForeignKey(“PrimaryImageID”)]
公共虚拟属性图像主图像{get;set;}
公共ICollection映像{get;set;}
}
公共类属性图像
{
[关键]
公共Guid ID{get;set;}
公共int属性ID{get;set;}
[外键(“属性ID”)]
公共虚拟财产属性{get;set;}
}
试试这个:

public class Property
{
  public int ID { get; set; }
  public string Name { get; set; }
  public Guid PrimaryImageID { get; set; }
  [ForeignKey("PrimaryImageID")]
  public virtual PropertyImage PrimaryImage { get; set; }

  public ICollection<PropertyImage> Images { get; set; }
}

public class PropertyImage
{
  [Key]
  public Guid ID { get; set; }
  public int PropertyID { get; set; }

  [ForeignKey("PropertyID")]
  public virtual Property Property { get; set; }
}
公共类属性
{
公共int ID{get;set;}
公共字符串名称{get;set;}
公共Guid PrimaryImageID{get;set;}
[ForeignKey(“PrimaryImageID”)]
公共虚拟属性图像主图像{get;set;}
公共ICollection映像{get;set;}
}
公共类属性图像
{
[关键]
公共Guid ID{get;set;}
公共int属性ID{get;set;}
[外键(“属性ID”)]
公共虚拟财产属性{get;set;}
}
首先,将“PrimaryImage”属性添加到属性类:

public class Property
{
    public int ID { get; set; }
    public string Name { get; set; }
    public Guid PrimaryImageID { get; set; }
    public virtual PropertyImage PrimaryImage { get; set; }

    public ICollection<PropertyImage> Images { get; set; }
}
然后,在line base.OnModelCreating(modelBuilder)之后,您可以编写:

modelBuilder.Entity<Property>().
    .HasRequired(x => x.PrimaryImage)
    .WithRequiredPrincipal();

modelBuilder.Entity<Property>().
    .HasMany(x => x.Images)
    .WithRequired(x => x.Property);
modelBuilder.Entity()。
.HasRequired(x=>x.PrimaryImage)
.WithRequiredPrincipal();
modelBuilder.Entity()。
.HasMany(x=>x.Images)
.WithRequired(x=>x.Property);
如果这是您想要的,那么我相信此代码允许您拥有所需的属性。希望有帮助

首先,将“PrimaryImage”属性添加到属性类:

public class Property
{
    public int ID { get; set; }
    public string Name { get; set; }
    public Guid PrimaryImageID { get; set; }
    public virtual PropertyImage PrimaryImage { get; set; }

    public ICollection<PropertyImage> Images { get; set; }
}
然后,在line base.OnModelCreating(modelBuilder)之后,您可以编写:

modelBuilder.Entity<Property>().
    .HasRequired(x => x.PrimaryImage)
    .WithRequiredPrincipal();

modelBuilder.Entity<Property>().
    .HasMany(x => x.Images)
    .WithRequired(x => x.Property);
modelBuilder.Entity()。
.HasRequired(x=>x.PrimaryImage)
.WithRequiredPrincipal();
modelBuilder.Entity()。
.HasMany(x=>x.Images)
.WithRequired(x=>x.Property);

如果这是您想要的,那么我相信此代码允许您拥有所需的属性。希望有帮助

如果您需要一个属性具有只能是适用于该属性的图像的主属性图像,则需要切换重点:

在输入图像并与要开始的属性相关之前,不能为属性设置主图像

除非存在要关联的属性,否则无法添加图像

因此,您需要将PrimaryImage属性设置为null,直到稍后设置

虽然PropertyImage依赖于属性,但属性不依赖于PropertyImage,因此不应是其记录中的外键

这意味着PrimaryImage的标志(布尔值)需要与PropertyImage一起存储,以指示哪一个图像是主图像

从属性中删除PrimaryImage ID,并在PropertyImage(IsPrimaryImage)上放置一个属性,以允许选择主图像

您可以通过UI处理唯一选择,也可以通过表上的唯一约束更正确地处理唯一选择

public class Property
{
    public int ID { get; set; }
    public string Name { get; set; }

    public ICollection<PropertyImage> Images { get; set; }
}

public class PropertyImage
{
    [Key]
    public Guid ID { get; set; }
    public int PropertyID { get; set; }
    public bool IsPrimaryImage { get;set; }

    public virtual Property Property { get; set; }
}
和检索当前主映像:

Property.PropertyImages.Where(p => p.IsPrimaryImage).Url

如果您需要一个属性具有只能是适用于该属性的图像的主属性图像,则需要切换重点:

在输入图像并与要开始的属性相关之前,不能为属性设置主图像

除非存在要关联的属性,否则无法添加图像

因此,您需要将PrimaryImage属性设置为null,直到稍后设置

虽然PropertyImage依赖于属性,但属性不依赖于PropertyImage,因此不应是其记录中的外键

这意味着PrimaryImage的标志(布尔值)需要与PropertyImage一起存储,以指示哪一个图像是主图像

从属性中删除PrimaryImage ID,并在PropertyImage(IsPrimaryImage)上放置一个属性,以允许选择主图像

您可以通过UI处理唯一选择,也可以通过表上的唯一约束更正确地处理唯一选择

public class Property
{
    public int ID { get; set; }
    public string Name { get; set; }

    public ICollection<PropertyImage> Images { get; set; }
}

public class PropertyImage
{
    [Key]
    public Guid ID { get; set; }
    public int PropertyID { get; set; }
    public bool IsPrimaryImage { get;set; }

    public virtual Property Property { get; set; }
}
和检索当前主映像:

Property.PropertyImages.Where(p => p.IsPrimaryImage).Url

您想创建与Flunt API的关系?如何保证主映像位于
属性的图像集合中?您想创建与Flunt API的关系?如何保证主映像位于
属性的图像集合中?可能可以,在这种情况下,我认为它不会对可维护性产生太大影响。他想要一个图像作为主图像。我想不出这怎么会在将来的某个时候让他的代码变得不可管理。问题是,会有多个属性,每个属性只能为具有匹配属性ID的图像设置一个主图像。