Pregunta

I'm a beginner programmer in C#, so I think that the solution of my question might be easy, but after looking for it for days, I have not found anything that worked for me.

I have a WP7 app that contains a DB created with SQL CE.

CLASS PorniBD.cs

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Data.Linq.Mapping;

namespace PhoneClassLibrary1
{
    [Table(Name = "Papilleros")]
    public class PorniBD
    {
        [Column(IsPrimaryKey = true, IsDbGenerated = true)]
        public int Id { get; set; }
        [Column(CanBeNull = false)]
        public String Nombre { get; set; }
        [Column(CanBeNull = false)]
        public String FechaNac { get; set; }
        [Column(CanBeNull = false)]
        public Boolean Activo { get; set; }
        [Column(CanBeNull = false)]
        public String Icono { get; set; }
    }
}

CLASS PorniContext.cs

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Data.Linq.Mapping;
using System.Data.Linq;

namespace PhoneClassLibrary1
{
    public class PorniContext : DataContext
    {
        public PorniContext(string connectionString) : base(connectionString)
        {
            //
        }

        public Table<PorniBD> Papilleros
        {
            get
            {
                return this.GetTable<PorniBD>();
            }
        }
    }
}

My app has a background agent created in another project like I have learned in this page: Link

Now, I need to read app DB from background agent, and this class contains following OnInvoke void:

protected override void OnInvoke(ScheduledTask task)
        {
            List<PorniBD> listapapilleros = new List<PorniBD>();
            using (PorniContext basedatos = new PorniContext("Data Source='isostore:/basedatos.sdf'"))
            {
                listapornis = basedatos.Papilleros.ToList();
            }

            // Launch a toast to show that the agent is running.
            // The toast will not be shown if the foreground application is running.

            ScheduledActionService.LaunchForTest(task.Name, TimeSpan.FromSeconds(10));
            NotifyComplete();
        }

but it's impossible because DataSource isolated is different in each project (I think), and I suppose that it's necessary to fix something more...

Thanks a lot for your help, and sorry if my English level is makes my explanation a bit difficult to understand...

¿Fue útil?

Solución

Simply create a third project, of type "Windows Phone Class Library". Move your database code in that third project, then reference it from both your main project and your background agent project.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top