如何使用LINQ to SQL更新数据库?

时间:2015-03-27 01:34:00

标签: c# linq linq-to-sql

我是使用LINQ的新手。我知道这个主题上有很多不同的帖子,但大多数都比较年长,不适合我的情况(或者我做错了其他事情)。

我有一个表单,在加载时会创建数据上下文。我有一个辅助表单,用户可以在其中选择路径,并在具有一行的配置表中更新该路径。

这是Configuration类的定义......

[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Configuration")]
public partial class Configuration : INotifyPropertyChanging, INotifyPropertyChanged
{

    private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

    private int _Id;

    private string _LegalRepository;

    private string _TitleRepository;

    private bool _AlwaysOpenOnDesktop;

#region Extensibility Method Definitions
partial void OnLoaded();
partial void OnValidate(System.Data.Linq.ChangeAction action);
partial void OnCreated();
partial void OnIdChanging(int value);
partial void OnIdChanged();
partial void OnLegalRepositoryChanging(string value);
partial void OnLegalRepositoryChanged();
partial void OnTitleRepositoryChanging(string value);
partial void OnTitleRepositoryChanged();
partial void OnAlwaysOpenOnDesktopChanging(bool value);
partial void OnAlwaysOpenOnDesktopChanged();
#endregion

    public Configuration()
    {
        OnCreated();
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Id", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
    public int Id
    {
        get
        {
            return this._Id;
        }
        set
        {
            if ((this._Id != value))
            {
                this.OnIdChanging(value);
                this.SendPropertyChanging();
                this._Id = value;
                this.SendPropertyChanged("Id");
                this.OnIdChanged();
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_LegalRepository", DbType="NVarChar(100) NOT NULL", CanBeNull=false)]
    public string LegalRepository
    {
        get
        {
            return this._LegalRepository;
        }
        set
        {
            if ((this._LegalRepository != value))
            {
                this.OnLegalRepositoryChanging(value);
                this.SendPropertyChanging();
                this._LegalRepository = value;
                this.SendPropertyChanged("LegalRepository");
                this.OnLegalRepositoryChanged();
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_TitleRepository", DbType="NVarChar(100) NOT NULL", CanBeNull=false)]
    public string TitleRepository
    {
        get
        {
            return this._TitleRepository;
        }
        set
        {
            if ((this._TitleRepository != value))
            {
                this.OnTitleRepositoryChanging(value);
                this.SendPropertyChanging();
                this._TitleRepository = value;
                this.SendPropertyChanged("TitleRepository");
                this.OnTitleRepositoryChanged();
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_AlwaysOpenOnDesktop", DbType="Bit NOT NULL")]
    public bool AlwaysOpenOnDesktop
    {
        get
        {
            return this._AlwaysOpenOnDesktop;
        }
        set
        {
            if ((this._AlwaysOpenOnDesktop != value))
            {
                this.OnAlwaysOpenOnDesktopChanging(value);
                this.SendPropertyChanging();
                this._AlwaysOpenOnDesktop = value;
                this.SendPropertyChanged("AlwaysOpenOnDesktop");
                this.OnAlwaysOpenOnDesktopChanged();
            }
        }
    }

创建数据库上下文......

public partial class fmFileSearch : Form
{
    public Configuration configuration = new Configuration();

    public FileSearchDataContext fileSearchDB = new FileSearchDataContext();

    public fmFileSearch()
    {
        InitializeComponent();

        this.WindowState = FormWindowState.Maximized;

        ReadConfiguration();
    }

我可以阅读这行......

configuration = (from c in fileSearchDB.Configurations where c.Id == 1 select c).SingleOrDefault();

但是当我尝试更新行时,它不会更新......

configuration.TitleRepository = locateRepository.SelectedPath;

fileSearchDB.SubmitChanges();

该表有一个主键,当我查看存储中的配置对象时,它具有新值。

为什么不赢得数据库更新?

更新:我添加了代码,以显示如何在我调用例程来读取行的类的构造函数中创建数据库上下文。

更新2:如果我以这种方式读取行,它就可以工作......

var query =
    from c in fileSearchDB.Configurations
        where c.Id == 1
        select c;

    foreach (Configuration c in query)
    {
        configuration = c;
    }

为什么?

1 个答案:

答案 0 :(得分:0)

确保此行上的fileSearchDB

configuration = (from c in fileSearchDB.Configurations where c.Id == 1 select c).SingleOrDefault();

实际上是此

中的相同数据库上下文
fileSearchDB.SubmitChanges();

因为第一行可能是一个db上下文而第二行中是另一个db上下文。

确保它们共享相同的数据库上下文,尝试进行以下操作:

using(var fileSearchDb = new MyFileSearchDbContext())
{
    var configuration = (from c in fileSearchDB.Configurations where c.Id == 1 select c).SingleOrDefault();
    configuration.TitleRepository = locateRepository.SelectedPath;
    fileSearchDB.SubmitChanges();

}