Linq to Sql - 创建数据库时未生成 FK
任何人都可以帮助我解决使用 DataContext.CreateDatabase() 函数创建数据库时,表 Track 和表 TrackArtist 之间没有生成外键的原因我的实体?我的表/列生成得很好,但我的关系不是作为 FK 约束生成的。
我试图在表之间创建一个 FK,以便 TrackArtist 表可以成为 Track 表的“查找”表。
[Table(Name="Track")]
public class Track
{
[Column (IsPrimaryKey=true, IsDbGenerated=true, AutoSync=AutoSync.OnInsert)]
public int Id { get; set; }
[Column]
private int TrackArtistId {get; set;}
private EntityRef<TrackArtist> _trackArtist;
[Association(Name="FK_Track_TrackArtist", ThisKey = "TrackArtistId", OtherKey="Id", Storage = "_trackArtist")]
public TrackArtist TrackArtist
{
get { return this._trackArtist.Entity; }
set { this._trackArtist.Entity = value;
TrackArtistId = value.Id;
}
}
[Column (CanBeNull=false)]
public string Description { get; set; }
}
[Table(Name="TrackArtist")]
public class TrackArtist
{
[Column(IsPrimaryKey=true, IsDbGenerated=true, AutoSync=AutoSync.OnInsert)]
internal int Id { get; set; }
[Column (CanBeNull=false)]
public string Name { get; set; }
}
Can anyone help me with the reason that a foreign key between table Track and table TrackArtist is not being generated when using the DataContext.CreateDatabase() function to create my database based on my entities? My tables/columns generate fine, but my relationship is not being generated as a FK constraint.
I'm trying to create a FK between the tables so that the TrackArtist table can be a 'lookup' table for the Track table.
[Table(Name="Track")]
public class Track
{
[Column (IsPrimaryKey=true, IsDbGenerated=true, AutoSync=AutoSync.OnInsert)]
public int Id { get; set; }
[Column]
private int TrackArtistId {get; set;}
private EntityRef<TrackArtist> _trackArtist;
[Association(Name="FK_Track_TrackArtist", ThisKey = "TrackArtistId", OtherKey="Id", Storage = "_trackArtist")]
public TrackArtist TrackArtist
{
get { return this._trackArtist.Entity; }
set { this._trackArtist.Entity = value;
TrackArtistId = value.Id;
}
}
[Column (CanBeNull=false)]
public string Description { get; set; }
}
[Table(Name="TrackArtist")]
public class TrackArtist
{
[Column(IsPrimaryKey=true, IsDbGenerated=true, AutoSync=AutoSync.OnInsert)]
internal int Id { get; set; }
[Column (CanBeNull=false)]
public string Name { get; set; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
就像 ColumnAttribute 的 IsPrimaryKey 属性在您的 Id 属性上为 true 一样,ColumnAttribute 的 IsForeignKey 属性在您的 TrackArtist 属性上也需要为 true。
请参阅此处有关 ColumnAttribute.IsForeignKey 的 MSDN 文档: http://msdn.microsoft.com/en-us/library/system.data.linq.mapping.associationattribute.isforeignkey.aspx
Just like the IsPrimaryKey property of ColumnAttribute is true on your Id property, the IsForeignKey property of ColumnAttribute needs to be true on your TrackArtist property.
See the MSDN documentation for ColumnAttribute.IsForeignKey here: http://msdn.microsoft.com/en-us/library/system.data.linq.mapping.associationattribute.isforeignkey.aspx
这可能不是您想要的,但值得一提。表关键字段的命名约定。我建议使用 TableNameId 作为主键名称。 TrackId、TrackArtistId。这种命名约定还应该可以帮助您发现拼写错误,而拼写错误并不罕见。此外,在“Id”的情况下,您通常可以避免与内部关键字和受保护关键字发生冲突。
This is probably not what you looking for but though worth to mention. A naming convention for table key fields. I would recommend using TableNameId as primary key name. TrackId, TrackArtistId. Such naming convention should also help you find typos, which not so seldom make cryptic errors. Also, in this case of 'Id' you generally avoid conflicts with internal and protected keywords.