I have 2 tables Users and Queries. They are connected via FK(UserId) in Queries table. I need to add queries added, for example, by user with login "Bob" to all users.
Here is a chunk of code i'm using:
public bool SaveUserQuery(string userName, Query query) {
var db = new UserDataClassesDataContext();
Table<User> users = db.Users;
if ( userName.ToLower() == "bob" ) {
foreach ( var user in users ) {
var tempQuery = new Query();
tempQuery.Name = query.Name;
tempQuery.FolderName = query.FolderName;
tempQuery.Layout = query.Layout;
tempQuery.Description = query.Description;
tempQuery.Query1 = query.Query1;
tempQuery.UserID = user.UserId;
try {
user.Queries.Add(q);
}
catch (Exception e) {
Logger.Log.Error("attach", e);
}
}
db.SubmitChanges();
return true;
}
}
It throws error when adding: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Queries_Users". The conflict occurred in database "OLAPUsers", table "dbo.Users", column 'UserId'.
How can i fix this or archive the goal i have?