Reputation: 19
I read a couple of posts related to InsertOnSubmit is not working but none of them resolve my issue. Below is my code, I have insert manually a record in my BackupPlan table and when i run a linq select the record from database and the 2 new records I tried to insert below returned and show in log, but when I checked in server explorer -> data connections -> table BackupPlan only the record that was manually inserted was there and not the new ones. I also tried updating existing record and delete with no success and without getting any exception. Furthermore I have added a column Id and set as a primary key to my BackupPlan table, checked db.GetChangeSet() and returns that 2 insert statements are pending, but db.SubmitChanges() runs after with no success. Any idea what I am missing here??? :(
using System.Data.Linq.Mapping;
namespace Storage.Models
{
[Table(Name = "BackupPlan")]
public class BackupPlan
{
[Column(IsPrimaryKey = true, IsDbGenerated = true)]
public int Id;
[Column]
public string Name;
[Column]
public string Description;
}
}
using System.Data.Linq;
using Storage.Models;
namespace Storage
{
public partial class Repository : DataContext
{
public Table<BackupPlan> BackupPlans;
public Repository(string connection):base(connection){}
}
}
using System;
using System.Data.Linq;
using System.Linq;
using Storage.Models;
using Storage.Properties;
namespace Storage
{
class Test
{
static void Main(string[] args)
{
try
{
var settings = new Settings();
var db = new Repository(settings.DatabaseConnectionString)
{Log = Console.Out, ObjectTrackingEnabled = true};
var backupPlan1 = new BackupPlan() {Description = "This is my first backup plan!", Name = "B Plan"};
db.BackupPlans.InsertOnSubmit(backupPlan1);
var backupPlan2 = new BackupPlan() {Description = "This is my first backup plan 2!", Name = "B22 Plan"};
db.BackupPlans.InsertOnSubmit(backupPlan2);
var t = db.GetChangeSet(); //shows that 2 inserts are pending
db.SubmitChanges(); // when i run this method i check my table but there are no any new records
var q = from b in db.BackupPlans
select b;
foreach (var backupPlan in q)
{
Console.WriteLine("\n{0}", backupPlan.Name);
}
Console.ReadKey();
}
catch(Exception e)
{
Console.WriteLine(e.Message);
Console.ReadKey();
}
}
}
}
Upvotes: 1
Views: 1707
Reputation: 596
http://dean-o.blogspot.com/2010/03/mind-your-copy-to-output-directory.html
You build your program, it copies the database file to Bin/Debug and you write to that copy.
The next time you build, it's going to overwrite the database you just wrote to with a fresh copy. Change it to do not copy.
Upvotes: 0