gpt4 book ai didi

entity-framework-4 - 使用 C# 和 Entity Framework CTP 4 的领域建模问题

转载 作者:行者123 更新时间:2023-12-04 15:04:17 26 4
gpt4 key购买 nike

我正在尝试建模一个包含照片集的相册。每个相册都有一组照片和一张拇指照片。这就是我所拥有的,但 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 在我的数据库中生成 1 对多,但它一直在创建相册和照片 (Album_Photos) 之间的引用表,但这不应该是多对多。任何帮助都会很棒。

最佳答案

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

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

解决方案:

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

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

这导致以下所需的架构:
alt text

关于entity-framework-4 - 使用 C# 和 Entity Framework CTP 4 的领域建模问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4355998/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com