6

我正在尝试建模一个包含照片集的相册。每个相册都有一组照片和一张拇指照片。这就是我所拥有的,但 EF 似乎不喜欢它:

public class Album : IEntity {
        private DateTime _dateCreated;

        public Album() {
            _dateCreated = SystemTime.Now();
            Photos = new List<Photo>();
        }
        public long Id { get; set; }
        public string Name { get; set; }
        public string Location { get; set; }
        public DateTime DateCreated
        {
            get { return _dateCreated; }
            set { _dateCreated = value; }
        }
        public virtual Site Site { get; set; }
        public virtual Photo Thumbnail { get; set; }
        public virtual ICollection<Photo> Photos { get; set; }
    }

public class Photo : IEntity {
            public Photo() {
                _dateCreated = SystemTime.Now();
            }
            private DateTime _dateCreated;
            public long Id { get; set; }
            public string Caption { get; set; }
            public string FileName { get; set; }
            public DateTime DateCreated
            {
                get { return _dateCreated; }
                set { _dateCreated = value; }
            }
            public virtual Album Album { get; set; }

        }

 public class AlbumMap : EntityConfiguration<Album>
    {
        public AlbumMap()
        {
            HasKey(x => x.Id);
            Property(x => x.Id).IsIdentity();
            Property(x => x.Location).IsVariableLength().HasMaxLength(80);
            Property(x => x.Name).IsVariableLength().HasMaxLength(80).IsRequired();
            Property(x => x.DateCreated);
            MapSingleType(a => new
            {
                a.Id,
                SiteId = a.Site.Id,
                ThumbnailId = a.Thumbnail.Id,
                a.Location,
                a.Name,
                a.DateCreated
            }).ToTable("Albums");
        }
    }

public class PhotoMap : EntityConfiguration<Photo>
    {
        public PhotoMap()
        {
            HasKey(x => x.Id);
            Property(x => x.Id).IsIdentity();
            Property(x => x.FileName).IsVariableLength().HasMaxLength(255).IsRequired();
            Property(x => x.Caption).IsVariableLength().HasMaxLength(255);
            Property(x => x.DateCreated);
            MapSingleType(a => new
            {
                a.Id,
                SiteAlbumId = a.Album.Id,
                a.FileName,
                a.Caption,
                a.DateCreated
            }).ToTable("AlbumPhotos");
        }
    }

我错过了什么或者这看起来对吗?我希望 EF 在我的数据库中生成一对多,但它会不断在相册和照片(Album_Photos)之间创建一个参考表,但这不应该是多对多。任何帮助都会很棒。

4

1 回答 1

6

按照惯例,EF 代码首先不会典型的一对多场景中创建链接表,之所以得到它,是因为 EF 将 Album 和 Photo 对象之间的关联视为一种多对多关联:
每个相册都有一组照片,而且每张照片都有一组相册,这张照片是其缩略图(尽管相关的导航属性没有在 Photo 类上明确指定,只有相册有缩略图属性)。

解决方案:

从 EF CTP4 开始,解决此问题的唯一方法是利用 Fluent API,但在此之前,我会稍微修改您的模型并为您的模型添加两个显式 FK,以便为您提供使用对象的最大灵活性。他们一直AlbumId在:Photo_ThumbnailIdAlbum

public class Photo {        
    public long Id { get; set; }
    public string Caption { get; set; }
    public string FileName { get; set; }
    public DateTime DateCreated { get; set; }

    public long AlbumId { get; set; }
    public virtual Album Album { get; set; }
}

public class Album {
    public long Id { get; set; }
    public string Name { get; set; }
    public string Location { get; set; }
    public DateTime DateCreated { get; set; }

    public long ThumbnailId { get; set; }
    public virtual Photo Thumbnail { get; set; }

    public virtual ICollection<Photo> Photos { get; set; }
}

public class PhotoMap : EntityConfiguration<Photo> {
    public PhotoMap() 
    {
        this.HasRequired(p => p.Album)
            .WithMany(a => a.Photos)
            .HasConstraint((p, a) => p.AlbumId == a.Id);

        Property(x => x.FileName).IsVariableLength().HasMaxLength(255)
                                                    .IsRequired();
        Property(x => x.Caption).IsVariableLength().HasMaxLength(255);
        MapSingleType(p => new {
            p.Id,
            SiteAlbumId = p.AlbumId,
            p.FileName,
            p.Caption,
            p.DateCreated
        })
        .ToTable("Photo");
    }
}

public class AlbumMap : EntityConfiguration<Album> {
    public AlbumMap()
    {
        this.HasRequired(a => a.Thumbnail)
            .WithMany()
            .WillCascadeOnDelete(false)
            .HasConstraint((a, p) => p.Id == a.ThumbnailId);

        Property(x => x.Location).IsVariableLength().HasMaxLength(80);
        Property(x => x.Name).IsVariableLength().HasMaxLength(80).IsRequired();
        MapSingleType(a => new {
            a.Id,
            a.ThumbnailId,
            a.Location,
            a.Name,
            a.DateCreated
        })
        .ToTable("Album");
    }
}


这导致以下所需的架构: 替代文字

于 2010-12-05T15:47:30.853 回答