简体   繁体   中英

Problem trying to create database for windows phone app

I'm trying to create a simple database to use sqlce in a windows phone app. I have a base class, and another set of classes that derive from it Here's what i got

public abstract class EntityBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }

        private int id;

        [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
        public int EntityId
        {
            get
            {
                return id;
            }
            set
            {
                if (id != value)
                {
                    id = value;
                    OnPropertyChanged("Id");
                }
            }
        }
    }

    [Table]
    public class Derived : EntityBase 
    {
        [Column]
        public string Description
        {
            get;
            set;
        }
    }

Then, I've got this class for datacontext purposes:

public class MyDataContext : 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 MyDataContext(string connectionString)
            : base(connectionString)
        { }

        public Table<Derived> Deriveds;
    }

And finally, here i'm trying to create the db :

private void Application_Launching(object sender, LaunchingEventArgs e)
        {
            using (MyDataContext db = new MyDataContext(MyDataContext.DBConnectionString))
            {
                if (db.DatabaseExists() == false)
                {
                    //Create the database -> here's the error
                    db.CreateDatabase();
                }
            }
        }

I'm getting the following error when trying to create the database :

Invalid column ID. [ EntityId ]

Yes, a very descriptive error message... Any ideas on what's wrong? I've been tampering around with the attributes in the column but to no avail.

[EDIT] : for what i've been testing, if i put the EntityId property in the derived class, it doesn't crash. This can be 2 things.. one, that I'm missing something else in the base class, or the other one, that the column attribute for a primary key must belong to the class, and can't belong to the parent (which would be a extremely horrible design decision, we can't use inheritance???). if someone can confirm this that would be appreciated

Ok, i think i found what was happening. In this implementation of SQLCe, we need to do the following approach when using inheritance :

http://msdn.microsoft.com/en-us/library/bb399352(v=VS.100).aspx

I don't particularly like it very much, the tables that it generates aren't normalized, but well, it's true that we shouldn't be using huge and complex datastores for WP7 apps, it's more to store some basic info that's too much to handle by isolated storage on itself.. Following this approach, it worked. I have now a collection of the base class, and i can put into it any of it's derived children. Then to retrieve them back, I use the discriminator in a linq to sql query to get one derived class or another.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM