我有两个类Song
和Album
。 Album
具有嵌套的歌曲集合。但在EF中,我必须建立关系。但Song
可以是单个或没有Album
。所以我想在数据库中创建类似的东西。在实体框架中定义集合而不创建关系
public class Song:IEntity
{
public Song()
{
Id = Guid.NewGuid().ToString();
}
public string Id { get; set; }
public string Artist { get; set; }
public string Title { get; set; }
}
public class Album:IEntity
{
public Album()
{
Id = Guid.NewGuid().ToString();
}
public string Id { get; set; }
public string Artist { get; set; }
public string Title { get; set; }
public DateTime Year { get; set; }
public virtual ICollection<Song> Songs { get; set; }
}
你没有一个主键。 Id属性每次都是一个新字符串。 – LiverpoolsNumber9 2014-09-28 12:21:52
不确定为什么你会把字符串当作关键字? – 2014-09-28 13:26:37