我在保存对数据库的更改方面存在问题,我正在使用LINQ2SQL映射.我已经基于以下教程实现了M:M关系(User <= UserRole => Role):http://www.codeproject.com/KB/linq/linqtutorial2.aspx#premain25
当我使用一个继承自DataContext并负责我所有域类的类时,一切正常,例如:
[数据库]公共类BookCatalog:DataContext {//创建静态DataContext以删除M:M Join记录私有静态DataContext contextForRemovedRecords = null;
public BookCatalog() : base("Data Source=KO2\\SQLSERVER;Initial Catalog=Katalog;Integrated Security=True") { }
public Table<User> Users;
public Table<Role> Roles;
public Table<UserRole> UserRoles;
public static void RemoveRecord<T>(T recordToRemove) where T : class
{
// Use the static contextForRemovedRecords
if (contextForRemovedRecords == null)
contextForRemovedRecords = new BookCatalog();
Table<T> tableData = contextForRemovedRecords.GetTable<T>();
var deleteRecord = tableData.SingleOrDefault(record => record == recordToRemove);
if (deleteRecord != null)
{
tableData.DeleteOnSubmit(deleteRecord);
}
}
// NEW method (not …Run Code Online (Sandbox Code Playgroud)