是否有必要SQL Server CE中的每个表必须具有主键?

时间:2015-02-23 05:34:37

标签: database linq windows-phone-8 linq-to-sql sql-server-ce

我正在创建一个简单的Windows Phone 8应用程序。要在本地存储数据,我使用的是Microsoft的ORM LINQ TO SQL。我假设它使用SQL Server CE Microsoft提供的本机数据库。我已经定义了一个存储州名称及其资本的表。

[Table]
public class State : INotifyPropertyChanged, INotifyPropertyChanging
{
    private string _name;

    public State(string name, string capital)
    {
        this.Name = name;
        this.Capital = capital;
    }

    [Column]
    public string Name
    {
        get
        {
            return _name;
        }
        set
        {
            if (_name != value)
            {
                NotifyPropertyChanging("Name");
                _name = value;
                NotifyPropertyChanged("Name");
            }
        }
    }

    private string _capital;

    [Column]
    public string Capital
    {
        get
        {
            return _capital;
        }
        set
        {
            if (_capital != value)
            {
                NotifyPropertyChanging("Capital");
                _capital = value;
                NotifyPropertyChanged("Capital");
            }
        }
    }


    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    // Used to notify the page that a data context property changed
    private void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    #endregion

    #region INotifyPropertyChanging Members

    public event PropertyChangingEventHandler PropertyChanging;

    // Used to notify the data context that a data context property is about to change
    private void NotifyPropertyChanging(string propertyName)
    {
        if (PropertyChanging != null)
        {
            PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
        }
    }

    #endregion
}

我也定义了DataBase Context类。

public class DbDataContext : DataContext
{
    // Specify the connection string as a static, used in main page and app.xaml.
    public static string DBConnectionString = "Data Source=isostore:/ToDo.sdf";
    // Pass the connection string to the base class.
    public DbDataContext(string connectionString): base(connectionString)
    { 

    }
    // Specify a single table for the to-do items.
    public Table<State> State_table;
}

现在,当我尝试在MainPage类的构造函数中插入一行时,它会抛出一个异常,说“System.InvalidOperationException”。

public MainPage()
    {

        InitializeComponent();
        State temp = new State("Maharashtra", "Mumbai");
        dbobj = new DbDataContext(DbDataContext.DBConnectionString);
        dbobj.State_table.InsertOnSubmit(temp); //This where it generates an exception
        dbobj.SubmitChanges();
    }

但是当我在我的班级“州”中重新定义属性“名称”时

[Column(IsPrimaryKey = true )]
public string Name

然后它不会抛出异常。 谁能告诉我为什么这很开心呢?

谢谢!

1 个答案:

答案 0 :(得分:0)

是的,每个表都需要有一个主键,以便每一行都可以唯一标识。

还建议添加Version列,这会大大加快db的性能:

[Column(IsVersion = true)]
#pragma warning disable 169
private Binary version;