我已经为Windows Phone 7应用程序创建了本地数据库,并使用msdn上的教程创建了一个表。我对第二张桌子有疑问,如何添加它?当我用Linq制作另一个类时,是否需要使用相同的datacontext类并仅添加另一个表?我尝试了很多尝试以与创建第一个表相同的方式创建它的方法,但似乎没有任何作用,我的应用程序崩溃了。请帮忙

最佳答案

假设程序在一个表上运行正常(因此您知道一个表的连接字符串和数据上下文都可以),那么在添加第二个表时是,您需要编写一个具有[Table]属性的附加类,并且您需要向数据上下文添加属性。

    public class ATestDataContext : DataContext
    {
        public ATestDataContext(string connectionString) : base(connectionString)
        {
        }

        public Table<FTable> FirstTable
        {
            get
            {
                return this.GetTable<FTable>();
            }
        }

        public Table<STable> SecondTable
        {
            get
            {
                return this.GetTable<STable>();
            }
        }
    }

[Table]
public class FTable : INotifyPropertyChanged, INotifyPropertyChanging
{...}

[Table]
public class STable : INotifyPropertyChanged, INotifyPropertyChanging
{...}


如果您要在表之间建立关系,例如主从关系,那么您的类中还需要其他内容。我遇到的最好的例子之一是:http://windowsphonegeek.com/articles/Windows-Phone-Mango-Local-Database-mapping-and-database-operations

关于c# - 本地数据库Windows Phone 7,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9961390/

10-17 01:21
查看更多