使用 C# 和实体框架 CTP 4 进行领域建模问题

发布于 2024-10-06 01:15:18 字数 2694 浏览 1 评论 0原文

我正在尝试制作一个包含照片集的相册的模型。每个相册都会有一组照片和一张拇指照片。这是我所拥有的,但 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) 之间创建一个引用表,但这不应该是多对多。任何帮助都会很棒。

I am trying to model out a album that has a collection of photos. Each Album will have a collection of Photos and a Photo that is a thumb. This is what I have but EF does not seem to like it:

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

Am I missing something or does this look right? I am expecting EF to generate a 1 to many in my database but it keeps creating a reference table between Album and Photos (Album_Photos) but this should not be a many-to-many. Any help would be great.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜警司 2024-10-13 01:15:18

按照惯例,EF 代码首先不会在典型的一对多场景中创建链接表,您得到它的原因是因为您的相册和照片对象之间的关联已被 EF 视为一种多对多关联:

每个相册都有一个照片集合,每个照片都有一个相册集合,该照片是其缩略图(尽管在照片类上没有明确指定相关的导航属性,并且只有相册具有缩略图属性)。

解决方案:

从 EF CTP4 开始,解决此问题的唯一方法是利用 Fluent API,但在此之前,我稍微修改您的模型并向模型添加两个显式 FK,以便为您提供处理对象的最终灵活性。它们是 Photo 上的 AlbumIdAlbum 上的 ThumbnailId

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

这会产生以下所需的架构:
替代文字

By convention, EF code first does not create a link table in typical one to many scenario, the reason that you are getting it is because your associations between Album and Photo objects has been taken by EF as being a kind of many to many association:

Each album has a collection of Photos and also each Photo has a collection of Albums that this photo is a thumbnail for (although the related navigation property is not explicitly specified on Photo class and only Album has a Thumbnail property).

Solution:

As of EF CTP4, the only way to fix this is by leveraging Fluent API, but before that, I modify your model a little bit and add two explicit FKs to your model to give you ultimate flexibility to work with your objects. They are AlbumId on Photo and ThumbnailId on Album:

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

This results to the following desired schema:
alt text

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文