Linq to Sql - FK not being generated when creating the database
source link: https://www.codesd.com/item/linq-to-sql-fk-not-being-generated-when-creating-the-database.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Linq to Sql - FK not being generated when creating the database
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; }
}
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
Recommend
-
6
How To Access SQL Generated By Entity Framework Core 3 – ChristianFindlay.comSkip to content Entity Framework Core (EF) converts exp...
-
11
Gunnar Peipman – Programming BlogASP.NET Core, Blazor, .NET, Azure, SharePoint, IoT A portal focused on Operatio...
-
12
Creating a SQL Server database on Windows Azure(Just to address a point of confusion. In the old days, Microsoft called their cloud-based SQL Server cloud service ‘Windows Azure’. Then they made their cloud service do virtual machines and app...
-
7
Error creating Azure SQL Database Posted on April 9, 2021 A quick post today, quite simply, the error message is:
-
15
Operator linq as * (asterisk) in sql advertisements I want to select all the fields of one table, and only specifics fields of a second table....
-
10
How to Query JSON Data using SQL, LINQ, and ADO. NET
-
10
The Linq-To-Sql method is not supported? advertisements I'm using a query that tries to find a matching unique...
-
8
The result of a prompt for "a sexy robot woman" using Stability AI's DreamStudioThis AI Tool Is Being Used to Make Freaky, Machine-Generated PornPeople are creating the DALL-E of porn with something...
-
6
Go for C# developers: LINQ When I worked in C# I loved LINQ. I also probably used it more than I should have. I have...
-
1
AI-Generated YouTube Videos Are Being Used To Spread Malware
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK