如何定义嵌套识别关系实体框架代码 [英] How to define nested Identifying Relationships Entity Framework code first

查看:154
本文介绍了如何定义嵌套识别关系实体框架代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个简单的测试应用程序中首先使用EF5代码来测试各种功能。我已经在代表一对多链接的两个实体之间定义了一个识别关系。在这里我定义一个PhotoCollection,它有许多子照片实体;

I'm using EF5 code first in a simple test application at the moment to test various functions. I have defined an 'identifying relationship' between two entities which represent a one-to-many link. Here I define a PhotoCollection that has many child Photo entities;

public class PhotoCollection
{
    public int Id { get; set; }
    public virtual ISet<Photo> Photos { get; private set; }

    public PhotoCollection()
    {
        Photos = new HashSet<Photo>();
    }
}

public class Photo
{
    [Key, ForeignKey("Parent"), Column(Order = 1)]
    public int PhotoCollectionId { get; set; }

    [Key, Column(Order = 2)]
    public int PhotoId { get; set; }

    public virtual PhotoCollection Parent { get; set; }


    [Required, MaxLength(200)]
    public string FilePath { get; set; }

    public Photo()
    {
    }

}

和我的OnModelCreating的实现包括;

and my implementation of OnModelCreating includes;

modelBuilder.Entity<Photo>().Property(p => p.PhotoId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

结果是当我删除一个PhotoCollection时,所有照片实体也被删除通过识别关系。

The result is that when I delete a PhotoCollection, all the photo entities are deleted as well which is provided by the 'identifying relationship'. Great.

我的问题是如何在此对象图中定义进一步的级别,假设我想将PhotoProperties作为一个从照片到收藏。在这种情况下,我想删除PhotoCollection,所有相应的Photo和PhotoProperty记录也将被删除。使用上述方法,是否有必要向指向PhotoCollection的PhotoProperty添加一个GrandParent属性?

My question is how do I define a further level in this object graph, let's say I want to a PhotoProperties as a one-to-many collection from Photo. In this case I'd like to delete PhotoCollection and the all appropriate Photo and PhotoProperty records will be deleted too. Using the approach above, wouldn't it be necessary to add a GrandParent property to PhotoProperty that pointed to the PhotoCollection?

我可以使用流畅的Api来实现相同的结果模型构建器?

Can I achieve the same result using the fluent Api in model builder?

我设法在线查找的唯一示例是单级父级>子级别。

The only examples I've managed to find online are for a single level parent>child hierarchies.

提前感谢

推荐答案

在我看来,这应该是有效的:

In my opinion this should work:

public class Photo
{
    [Key, ForeignKey("Parent"), Column(Order = 1)]
    public int PhotoCollectionId { get; set; }

    [Key, Column(Order = 2)]
    public int PhotoId { get; set; }

    public virtual PhotoCollection Parent { get; set; }

    public virtual ISet<PhotoProperty> PhotoProperties { get; private set; }

    //...
}

public class PhotoProperty
{
    [Key, ForeignKey("Parent"), Column(Order = 1)]
    public int PhotoCollectionId { get; set; }

    [Key, ForeignKey("Parent"), Column(Order = 2)]
    public int PhotoId { get; set; }

    [Key, Column(Order = 3)]
    public int PhotoPropertyId { get; set; }

    public virtual Photo Parent { get; set; }

    //...
}

请注意 PhotoCollectionId PhotoProperty 中不涉及 PhotoCollection 是复合外键(PhotoCollectionId,PhotoId)的一部分,指的是照片

Note that PhotoCollectionId in PhotoProperty doesn't refer to a PhotoCollection but is part of the composite foreign key (PhotoCollectionId,PhotoId) that refers to Photo.

是的,您可以使用Fluent API定义整个映射:

And yes, you can define the whole mapping with Fluent API:

modelBuilder.Entity<PhotoCollection>()
    .HasMany(pc => pc.Photos)
    .WithRequired(p => p.Parent)
    .HasForeignKey(p => p.PhotoCollectionId);

modelBuilder.Entity<Photo>()
    .HasKey(p => new { p.PhotoCollectionId, p.PhotoId });

modelBuilder.Entity<Photo>()
    .Property(p => p.PhotoId)
    .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

modelBuilder.Entity<Photo>()
    .HasMany(p => p.PhotoProperties)
    .WithRequired(pp => pp.Parent)
    .HasForeignKey(pp => new { pp.PhotoCollectionId, pp.PhotoId });

modelBuilder.Entity<PhotoProperty>()
    .HasKey(pp => new { pp.PhotoCollectionId, pp.PhotoId, pp.PhotoPropertyId });

modelBuilder.Entity<PhotoProperty>()
    .Property(pp => pp.PhotoPropertyId)
    .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

这篇关于如何定义嵌套识别关系实体框架代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆