none
Création de table + création controller/Objet

    Discussion générale

  • Bonjour,

    J'ai ajouté une table dans ma BDD Azure dans visual studio et j'arrive pas  à l'utiliser j'ai comme erreur : "the ressource are looking for hs been removed, had its name changed, or is temporarily unavaliable."

    voilà ma classe dataobject:

    using Microsoft.Azure.Mobile.Server;
    
    
    namespace ChantierService.DataObjects
    {
        public class AlertMateriel : EntityData
        {
            public string IdMateriel { get; set; }
    
            public string IdDemandeur { get; set; }
    
            public string Demande { get; set; }
            public string IdAssignMat { get; set; }
    
            public string IdReceveur { get; set; }
            public bool Vu { get; set; }
        }
    }

    Mon controller:

    using System.Linq;
    using System.Threading.Tasks;
    using System.Web.Http;
    using System.Web.Http.Controllers;
    using System.Web.Http.OData;
    using Microsoft.Azure.Mobile.Server;
    using ChantierService.DataObjects;
    using ChantierService.Models;
    
    namespace ChantierService.Controllers
    {
        public class AlertMaterielController : TableController<AlertMateriel>
        {
            protected override void Initialize(HttpControllerContext controllerContext)
            {
                base.Initialize(controllerContext);
                ChantierContext context = new ChantierContext();
                DomainManager = new EntityDomainManager<AlertMateriel>(context, Request);
            }
    
            // GET tables/AlertMateriel
            public IQueryable<AlertMateriel> GetAllAlertMateriel()
            {
                return Query(); 
            }
    
            // GET tables/AlertMateriel/48D68C86-6EA6-4C25-AA33-223FC9A27959
            public SingleResult<AlertMateriel> GetAlertMateriel(string id)
            {
                return Lookup(id);
            }
    
            // PATCH tables/AlertMateriel/48D68C86-6EA6-4C25-AA33-223FC9A27959
            public Task<AlertMateriel> PatchAlertMateriel(string id, Delta<AlertMateriel> patch)
            {
                 return UpdateAsync(id, patch);
            }
    
            // POST tables/AlertMateriel
            public async Task<IHttpActionResult> PostAlertMateriel(AlertMateriel item)
            {
                AlertMateriel current = await InsertAsync(item);
                return CreatedAtRoute("Tables", new { id = current.Id }, current);
            }
    
            // DELETE tables/AlertMateriel/48D68C86-6EA6-4C25-AA33-223FC9A27959
            public Task DeleteAlertMateriel(string id)
            {
                 return DeleteAsync(id);
            }
        }
    }
    

    Dans mon starup:

       List<AlertMateriel> AlertMateriels = new List<AlertMateriel>
                {
                    new AlertMateriel { Id = Guid.NewGuid().ToString()},
                };
    
                foreach (AlertMateriel alermateriel in AlertMateriels)
                {
                    context.Set<AlertMateriel>().Add(alermateriel);
                }

    Dans mon context:

     public DbSet<AlertMateriel> AlertMateriels { get; set; }

    Classe objet dans mon appli:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    using Android.App;
    using Android.Content;
    using Android.OS;
    using Android.Runtime;
    using Android.Views;
    using Android.Widget;
    using System.Globalization;
    using Newtonsoft.Json;
    using Java.Lang;
    namespace Chantier
    {
       public class AlertMateriel
        {
            public string Id { get; set; }
            [JsonProperty(PropertyName = "IdMateriel")]
            public string IdMateriel { get; set; }
    
            [JsonProperty(PropertyName = "IdDemandeur")]
            public string IdDemandeur { get; set; }
    
            [JsonProperty(PropertyName = "Demande")]
            public string Demande { get; set; }
    
            [JsonProperty(PropertyName = "IdAssignMat")]
            public string IdAssignMat { get; set; }
    
            [JsonProperty(PropertyName = "IdReceveur")]
            public string IdReceveur { get; set; }
    
            [JsonProperty(PropertyName = "Deleted")]
            public string Deleted { get; set; }
    
    
            [JsonProperty(PropertyName = "VU")]
            public bool VU { get; set; }
    
           
        }
        public class AlertMAterielWrapper : Java.Lang.Object
        {
    
            public AlertMAterielWrapper(AlertMateriel item)
            {
                MyItem3 = item;
            }
            public AlertMateriel MyItem3 { get; private set; }
        }
    }

    Mon Activity:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    using Android.App;
    using Android.Content;
    using Android.OS;
    using Android.Runtime;
    using Android.Views;
    using Android.Widget;
    using Android.Util;
    using Microsoft.IdentityModel.Clients.ActiveDirectory;
    
    using Android.Support.V4.View;
    using Android.Support.V7.App;
    using Android.Support.V7.Widget;
    
    using System.Net.Http;
    using System.Threading.Tasks;
    using Microsoft.WindowsAzure.MobileServices;
    using Microsoft.WindowsAzure.MobileServices.Sync;
    using Microsoft.WindowsAzure.MobileServices.SQLiteStore;
    using System.IO;
    
    
    namespace Chantier
    {
        [Activity(Label = "@string/activity_chantiercdt", Theme = "@android:style/Theme.Black.NoTitleBar.Fullscreen")]
        public class DefMatTable : Activity
        {
            private IMobileServiceSyncTable<AlertMateriel> AlertMaterielTable;
            private IMobileServiceSyncTable<AssignMaterielDB> AssignMaterielTable;
            private IMobileServiceSyncTable<Chantiers> ChantiersTable;
            private IMobileServiceSyncTable<MaterielDB> MaterielTable;
            private IMobileServiceSyncTable<Personnel> PersonnelTable;
            private TableLayout _table;
            private MobileServiceClient client;
            private ArrayAdapter _adapter3;
            private string idengin="";
            private string idassign = "";
            private string iddemandeur = "";
            private string idreceveur = "";
    
            /*
            private TextView Engin;
            private TextView Chantier;
            private TextView Cdt;
            private TextView datedu;
            private TextView dateau;
            private ArrayAdapter _adapter;*/
    
            const string applicationURL = @"URL";
           // const string applicationKey = @"KeyAppli";
            public List<string> assignmatod { get; private set; }
            const string localDbFilename = "localstore.db";
            private ProgressDialog progress;
    
            protected override async void OnCreate(Bundle savedInstanceState)
            {
                base.OnCreate(savedInstanceState);
                SetContentView(Resource.Layout.Layout_DemandeMat2);
                this.Window.AddFlags(WindowManagerFlags.Fullscreen);
                this.Window.ClearFlags(WindowManagerFlags.Fullscreen);
                //this.Title += " " + ;
                CurrentPlatform.Init();
                client = new MobileServiceClient(applicationURL);
                await InitLocalStoreAsync();
                ChantiersTable = client.GetSyncTable<Chantiers>();
                AssignMaterielTable = client.GetSyncTable<AssignMaterielDB>();
                MaterielTable = client.GetSyncTable<MaterielDB>();
                PersonnelTable = client.GetSyncTable<Personnel>();
                AlertMaterielTable = client.GetSyncTable<AlertMateriel>();
                // Create your application here
                
               
                await SyncAsync(pullData: true);
                string idass = Intent.GetStringExtra("IdAssignMat");
                AssignMaterielDB assign = await AssignMaterielTable.LookupAsync(idass);
               
                _table = FindViewById<TableLayout>(Resource.Id.table_main);
               TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(
                    ViewGroup.LayoutParams.WrapContent,
                    ViewGroup.LayoutParams.WrapContent
                    );
    
                List<string> assignmatod = new List<string>();
                try
                {
                    //string idass = Intent.GetStringExtra("IdAssignMat");
                    string IdUser = Intent.GetStringExtra("IdUser");
                    string Estcdt = Intent.GetStringExtra("iscdt");
                    
                    if (Estcdt == "True")
                    {
                        var thelist = await AssignMaterielTable.Where(idmat => idmat.Id == idass).Take(1).ToListAsync();
    
                        //var thelist2 = await AssignPersonnelTable.Where(n => n.IdUser == IdUser).ToListAsync();
    
                        foreach (AssignMaterielDB item in thelist)
                        {
    
                            var chantier = await ChantiersTable.Where(idchat => idchat.Id == item.IdChantier).Take(1).ToListAsync();
    
                            var Matthelist = await MaterielTable.Where(Idmat => Idmat.Id == item.IdEngin).Take(1).ToListAsync();
                            foreach (MaterielDB mat in Matthelist)
                            {
                                foreach (Chantiers ch in chantier)
                                {
                                    var listper = await PersonnelTable.Where(Idmat => Idmat.Id == ch.Cdt).ToListAsync();
                                    foreach (Personnel pe in listper)
                                    {
                                        assignmatod.Add(item.Id);
                                        TableRow tableR = new TableRow(this);
                                        TextView text = new TextView(this);
                                        text.SetText(ch.NomChantier.ToCharArray(), 0, ch.NomChantier.Length);
                                        text.Gravity = GravityFlags.Center;
                                        tableR.AddView(text,0);
                                        TextView text2 = new TextView(this);
                                        text2.Gravity = GravityFlags.Center;
                                        string cdttr = pe.Pers_Prenom + " " + pe.Pers_Nom;
                                        text2.SetText(cdttr.ToCharArray(), 0,cdttr.Length);
                                        tableR.AddView(text2, 1);
                                        TextView text3 = new TextView(this);
                                        text3.Gravity = GravityFlags.Center;
                                        text3.SetText(item.DateDebut.ToString("d").ToCharArray(), 0, item.DateDebut.ToString("d").Length);
                                        tableR.AddView(text3, 2);
                                        TextView text4 = new TextView(this);
                                        text3.Gravity = GravityFlags.Center;
                                        text4.SetText(item.DateFin.ToString("d").ToCharArray(), 0, item.DateFin.ToString("d").Length);
                                        tableR.AddView(text4, 3);
                                        tableR.Id =View.GenerateViewId();
                                        tableR.Clickable = true;
                                        idengin = item.IdEngin;
                                        idassign = idass;
                                        iddemandeur = IdUser;
                                        idreceveur = ch.Cdt;
                                        tableR.Click += HandleClick;
                                        _table.AddView(tableR);
                                        //    _listView.ItemClick += _listView_ItemClick;
    
                                    }
                                }
                            }
                        }
                    }
    
                    _adapter3 = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, assignmatod);
                   
                }
                catch (MobileServiceInvalidOperationException e)
                {
                    Console.Error.WriteLine(@"ERROR {0}", e.Message);
                    //  return null;
                }
               
    
            }
    
         
    
            private   void HandleClick(object sender, EventArgs e)
             {
                 var clickedTableRow = sender as TableRow;
                 int s = clickedTableRow.Id;
                 var tag = clickedTableRow.Tag;
                string ideng = idengin;
                string idass = idassign;
                string idrecev = idreceveur;
                string iddeman = iddemandeur;
                 string arg = _adapter3.GetItem(s-1).ToString();
                string message = "Envoyer une demande pour cette engin sur la période concernée?";
               CreateAndShowDialogDemande(message, "DEMANDE ENGIN", ideng, iddeman, idass, idrecev);
    
    
               Toast.MakeText(this, tag + " idassign "+ s+ "arg " + arg, ToastLength.Long).Show();
    
             }
    
    
            private async void CreateAndShowDialogDemande(string message, string title, string idmat, string IdDemande, string IdAssignMat, string Idreceveur )
            {
                Android.App.AlertDialog.Builder builder = new Android.App.AlertDialog.Builder(this);
                string IdDuChantier = Intent.GetStringExtra("IdDuChantier");
                builder.SetMessage(message);
                builder.SetTitle(title);
                builder.Create();
                builder.SetPositiveButton("Faire la demande ?", async (senderAlert, args) =>
                {
    
                    var Demande = new AlertMateriel
                    {
    
                        IdMateriel = idmat,
                        IdDemandeur = IdDemande,
                        IdAssignMat = IdAssignMat,
                        IdReceveur = Idreceveur,
                        VU = false,
                     //   Deleted = false,
                        
    
    
                    };
                    try
                    {
                        progress = new Android.App.ProgressDialog(this);
                        progress.Indeterminate = true;
                        progress.SetProgressStyle(Android.App.ProgressDialogStyle.Spinner);
                        progress.SetMessage("Envoie de la Demande ...");
                        progress.SetCancelable(false);
                        progress.Show();
                        await AlertMaterielTable.InsertAsync(Demande);
                        await SyncAsync(pullData: true);
                        progress.Cancel();
                        CreateAndShowDialog("Demande", "Votre demande a été envoyé...");
                        // Toast.MakeText(this, "Votre demande a été envoyé", ToastLength.Long).Show();
                        //await client.SyncContext.PushAsync();
                    }
                    catch (Exception e)
                    {
                        CreateAndShowDialog(e.ToString(), "Error");
                    }
                    
                   
                    
                });
                builder.SetNegativeButton("Annuler", (senderAlert, args) =>
                {
                    builder.Dispose();
                });
    
    
    
                builder.Show();
    
            }
    
            private void CreateAndShowDialog(String title, string message)
            {
                Android.App.AlertDialog.Builder builder = new Android.App.AlertDialog.Builder(this);
    
                builder.SetMessage(message);
                builder.SetTitle(title);
                builder.SetNegativeButton("Rester sur la page", (senderAlert, args) =>
                {
                    builder.Dispose();
                });
                builder.Create().Show();
    
    
            }
            private void CreateAndShowDialog2(string message, string title)
            {
                Android.App.AlertDialog.Builder builder = new Android.App.AlertDialog.Builder(this);
    
                builder.SetMessage(message);
                builder.SetTitle(title);
                builder.Create().Show();
            }
    
    
            private async Task InitLocalStoreAsync()
            {
                // new code to initialize the SQLite store
                string path = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), localDbFilename);
    
                if (!File.Exists(path))
                {
                    File.Create(path).Dispose();
                }
    
                var store = new MobileServiceSQLiteStore(path);
                store.DefineTable<AlertMateriel>();
                store.DefineTable<Personnel>();
                store.DefineTable<Chantiers>();
                store.DefineTable<AssignMaterielDB>();
                store.DefineTable<MaterielDB>();
    
                // Uses the default conflict handler, which fails on conflict
                // To use a different conflict handler, pass a parameter to InitializeAsync. For more details, see http://go.microsoft.com/fwlink/?LinkId=521416
                await client.SyncContext.InitializeAsync(store);
            }
            protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
            {
                base.OnActivityResult(requestCode, resultCode, data);
                AuthenticationAgentContinuationHelper.SetAuthenticationAgentContinuationEventArgs(requestCode, resultCode, data);
            }
    
           
            private async Task SyncAsync(bool pullData = false)
            {
                try
                {
                    await client.SyncContext.PushAsync();
                    
                    await ChantiersTable.PullAsync("allTodoItems", ChantiersTable.CreateQuery()); // query ID is used for incremental sync
                    await AssignMaterielTable.PullAsync("allMat", AssignMaterielTable.CreateQuery());
                    await MaterielTable.PullAsync("alltvx", MaterielTable.CreateQuery());
                    await PersonnelTable.PullAsync("allUsers", PersonnelTable.CreateQuery());
                    await AlertMaterielTable.PullAsync("AlertMateirel", AlertMaterielTable.CreateQuery());
                }
                catch (Java.Net.MalformedURLException)
                {
                    CreateAndShowDialog("", "Error");
                }
                catch (Exception e)
                {
                    Console.WriteLine("error");
                    Console.WriteLine( e.GetType().Name);
                    Console.WriteLine(e);
                    // CreateAndShowDialog( "Error", e.ToString());
                }
            }
        }
    
    }  
        
    

    Merci d'avance

    mardi 2 mai 2017 08:17

Toutes les réponses