我阅读了几篇与 InsertOnSubmit is not working 相关的帖子,但没有一个能解决我的问题。下面是我的代码,我在我的 BackupPlan 表中手动插入了一条记录,当我运行 linq 时,从数据库中选择记录,我尝试在下面插入的 2 条新记录返回并显示在日志中,但是当我签入服务器资源管理器时 - > 数据连接 -> 表 BackupPlan 只有手动插入的记录在那里,而不是新记录。我还尝试更新现有记录并删除,但没有成功,也没有任何异常。此外,我添加了一个列 Id 并将其设置为我的 BackupPlan 表的主键,检查了 db.GetChangeSet() 并返回 2 个插入语句处于挂起状态,但 db.SubmitChanges() 运行后没有成功。知道我在这里缺少什么吗???:(
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();
}
}
}
}