[英]Is it necessary that every table in SQL Server CE must have a primary key?
I am creating a simple windows phone 8 application. 我正在创建一个简单的Windows Phone 8应用程序。 To store data locally I am using Microsoft's ORM LINQ TO SQL. 为了在本地存储数据,我正在使用Microsoft的ORM LINQ TO SQL。 I assume that it uses SQL Server CE the native database provided by Microsoft. 我假设它使用Microsoft提供的本机数据库SQL Server CE。 I have defined a table that stores name of the state and its capital. 我定义了一个表,用于存储州名及其首都。
[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
}
I have defined the DataBase Context class as well. 我也定义了数据库上下文类。
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;
}
Now when I try to insert one row in the Constructor of the MainPage class, It throws an exception saying "System.InvalidOperationException". 现在,当我尝试在MainPage类的Constructor中插入一行时,它将引发一个异常,提示“ 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();
}
But when I Redefine the Property "Name" in my class "State" as 但是当我在类“州”中将属性“名称”重新定义为
[Column(IsPrimaryKey = true )]
public string Name
Then it doesnt throw the exception. 然后,它不会引发异常。 Can anyone tell me why this is happnening? 谁能告诉我为什么这令人惊讶吗?
Thanks ! 谢谢 !
Yes each table is required to have a primary Key so that each row can be uniquely identified. 是的,每个表都需要有一个主键,以便可以唯一地标识每一行。
It's also recommended to add a Version column, which dramatically speeds up the performance of the db: 还建议添加“版本”列,该列可大大提高数据库的性能:
[Column(IsVersion = true)]
#pragma warning disable 169
private Binary version;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.