【发布时间】:2012-07-05 17:16:28
【问题描述】:
在我的数据库中,我有一个一对多的关系(乐队有很多专辑)。但是,当 Album 中的外键受到限制时,这种关系就变成了一对一的关系。
public class Band
{
[Key]
public int BandID { get; set; }
//Other fun attributes
public virtual ICollection<Album> Albums { get; set; }
}
public class Album
{
[Key]
public int AlbumID { get; set; }
public int BandID { get; set; }
//Other fun attributes
//Some other foreign key
public int SomeOtherKey { get; set; }
}
应该生成的SQL
SELECT * FROM Band
LEFT JOIN Album ON (Band.BandID = Album.AlbumID AND Album.SomeOtherKey = 12)
我的问题是我是否应该在 Band 中拥有另一个导航属性 public virtual Album Album,或者因为这并不总是正确的那将是一个坏主意?我应该使用 LINQ 吗?使用实体框架和模型完成此任务的最简单方法是什么?
【问题讨论】:
标签: c# .net sql-server linq entity-framework