gpt4 book ai didi

entity-framework - 用于轨道和专辑模型的 EF5 Code First Fluent EntityTypeConfiguration

转载 作者:行者123 更新时间:2023-12-05 01:09:29 24 4
gpt4 key购买 nike

我正在为音乐轨道和专辑建模,其中专辑有很多轨道,轨道只能在一张专辑中,并使用连接表指定它在专辑列表中的位置。

这是我的模型:

public class Track
{
public int Id { get; set; }
public string Name { get; set; }

public int AlbumTrackId { get; set; }
public virtual AlbumTrack AlbumTrack { get; set; }
}

public class Album
{
public int Id { get; set; }
public string Name { get; set; }

public virtual ICollection<AlbumTrack> AlbumTracks { get; set; }
}

public class AlbumTrack
{
public int AlbumId { get; set; }
public virtual Album Album { get; set; }

public int TrackId { get; set; }
public virtual Track Track { get; set; }

public int Position { get; set; }
}

和我的 EntityTypeConfiguration
public class AlbumTrackConfiguration : EntityTypeConfiguration<AlbumTrack>
{

public AlbumTrackConfiguration()
{
// AlbumTrack has a composite key
HasKey(at => new {at.AlbumId, at.TrackId});

// AlbumTrack has one Album, Albums have many AlbumTracks
HasRequired(at => at.Album)
.WithMany(a => a.AlbumTracks)
.HasForeignKey(at => at.AlbumId)
.WillCascadeOnDelete(true);

// AlbumTrack has one Track, Tracks have one AlbumTrack
HasRequired(at => at.Track)
.WithRequiredPrincipal(t=>t.AlbumTrack)
.WillCascadeOnDelete(true);
}

}

Albums 和 AlbumTracks 之间的一对多关系很好,但除了预期的
[AlbumTrackId] [int] NOT NULL,

Code First 不断添加
[AlbumTrack_AlbumId] [int] NOT NULL,
[AlbumTrack_TrackId] [int] NOT NULL

到轨道表。

如何更好地建模轨道到专辑轨道的关系,以便只有我指定的属性在 db 字段中转换?

[是的,在我的世界中,一首轨道只能存在于一张专辑中!]

谢谢。

最佳答案

制作 Track 的位置部分正如建议的那样有意义。
但是,如果您仍然希望您的“加入”表正常工作-我个人总是宁愿将轨道、专辑与他们的“关联”(即位置等)分开...

为此,您需要稍微重新组织一下您的人际关系。 EF/CF 无法构建它 - 因为你要求它的东西不是 native 支持或预期的。联接、索引表总是期待另一侧的“多重性”。

Basically, your AlbumTrack is no longer a 'join' table - but is just a one-on-one with the Track + you have the Album fk from the AlbumTrack.



考虑到这一点,您可以执行以下操作 - 这将创建列,索引恰到好处......
public class Track
{
public int Id { get; set; }
public string Name { get; set; }
// public int AlbumTrackId { get; set; }
public virtual AlbumTrack AlbumTrack { get; set; }
}
public class AlbumTrack
{
public int TrackId { get; set; } // <== this is a single primary
public virtual Track Track { get; set; }

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

public int Position { get; set; }
}
public class Album
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<AlbumTrack> AlbumTracks { get; set; }
}

...以及流畅的配置:
modelBuilder.Entity<AlbumTrack>()
.HasKey(at => new { at.TrackId });
//.HasKey(at => new { at.AlbumId, at.TrackId });

modelBuilder.Entity<AlbumTrack>()
.HasRequired(at => at.Track)
.WithOptional(a => a.AlbumTrack);
// .WithRequiredPrincipal(x => x.AlbumTrack);

modelBuilder.Entity<AlbumTrack>()
.HasRequired(at => at.Album)
.WithMany(a => a.AlbumTracks)
.HasForeignKey(at => at.AlbumId)
.WillCascadeOnDelete(true);

...你可以像这样使用它:
var album1 = db.Albums.Add(new Album { Name = "Track1", });

var tr1 = db.Tracks.Add(new Track { Name = "Track1", });
var tr2 = db.Tracks.Add(new Track { Name = "Track2", });
var tr3 = db.Tracks.Add(new Track { Name = "Track3", });
var tr4 = db.Tracks.Add(new Track { Name = "Track4", });
var tr5 = db.Tracks.Add(new Track { Name = "Track5", });

db.AlbumTracks.Add(new AlbumTrack { Track = tr1, Album = album1, Position = 1 });
db.AlbumTracks.Add(new AlbumTrack { Track = tr2, Album = album1, Position = 2 });
db.AlbumTracks.Add(new AlbumTrack { Track = tr3, Album = album1, Position = 3 });
db.AlbumTracks.Add(new AlbumTrack { Track = tr4, Album = album1, Position = 4 });
db.AlbumTracks.Add(new AlbumTrack { Track = tr5, Album = album1, Position = 5 });

db.SaveChanges();

(或者您可以只“添加”专辑轨道,其余的将进入)。

注:您不再需要复合键 - 因为您的 AlbumTrack 基本上只依赖于轨道 - 即您有 only one record对于每首轨道 - 附有“专辑”。
你也不需要 // public int AlbumTrackId { get; set; }因为那是相反的一面,fk 在专辑轨道上。这在旧设置中也是错误的(因为 fk 需要“两个键”来连接表)。

而且,要补充的是,整个结构基本上“建议”您只是将 Track 表“拆分”为两个一对一的表,并且您也可以只使用 Track/Album。但这有其自身的优势 - 虽然您有额外的连接和读/写(不利的一面),但您可以获得一些灵活性 - 或者例如稍后您可以通过普通连接表等将它们关联起来。

关于entity-framework - 用于轨道和专辑模型的 EF5 Code First Fluent EntityTypeConfiguration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15743548/

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