C# Xamarin: Sqlite table from Mappings classes (Entitytypeconfiguration<Class>)

Asked

Viewed 113 times

0

I’m trying to create some tables on sqlite with VS Community 2017 (c# and Xamarin android) from classes that are defined by Entitytypeconfiguration. Below I try to better detail my difficulty. I already appreciate any help.

** Commom properties
using System;
namespace Agenda.Domain.Common
{
    public class CommonProperties
    {
        public DateTime? Datalt { get; set; }
    }
}

*-*-*-*-*-*-*-*-*-*-*-   

** Classe Anêmica **
using Agenda.Domain.Common;
using System;
namespace Agenda.Domain
{
    public class User_Mobile : CommonProperties
    {
        public decimal Pk { get; set; }
        public string id { get; set; }
        public string ativo { get; set; }
        public string Senha { get; set; }
    }
}

*-*-*-*-*-*-*-*-*-*-*-
** Mapeamento da Classe User_Mobile **
using Agenda.Domain;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;
namespace Agenda.Infra.Mappings
{
    public class User_MobileMap : EntityTypeConfiguration
    {
        public User_MobileMap()
        {
            ToTable("user_mobile"); // Tabela no banco de Dados SQLServer (WebAPI) no WebApi funciona corretamente.
            HasKey(x => x.Pk);
            Property(x => x.Pk).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            Property(x => x.id).HasMaxLength(100).IsRequired();
            Property(x => x.ativo).HasMaxLength(1).IsRequired();
            Property(x => x.Senha).HasMaxLength(50).IsRequired();
        }
    }
}

*-*-*-*-*-*-*-*-*-*-*-
** Classe DBRepositório **
using Android.Util;
using SQLite;
using Agenda.Infra.Mappings;
using System;
using System.IO;
namespace Agenda.ManMob.DataBase
{
    public class DBRepositorio
    {
        // Cria a base de dados caso não exista.
        string sPath = "";
        public DBRepositorio()
        {
            sPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "DBSTN.db3");
        }
        public bool CreateTable()
        {
            try
            {
                using (var conn = new SQLiteConnection(sPath))
                {
                    conn.CreateTable();  ==>> Aqui está o problema.
                                        // no acompanhamento do processo (DEBUG) o nome da tabela passada para o conn.CreateTable é o nome da classe mapping (User_MobileMap) quando deveria ser o nome apontado em ToTable("user_mobile").
                                        // Como o nome difere da classe anêmica que conhece o banco de dados SQLServer me retorna um erro de que nenhuma coluna foi encontrada para criar a tabela.
                                        // Se puder me auxiliar neste ponto, ficarei muito grato.
                                        // Mensagem Original: "Cannot create a table with zero columns (does 'Agenda.Infra.Mappings.User_MobileMap' have public properties?)"
                    return true;
                }
            }
            catch (SQLiteException SQLError)
            {
                Log.Info("SQLite Ex.", SQLError.Message);
                return false;
            }
        }
        public bool CreateDB()
        {
            try
            {
                using (var conn = new SQLiteConnection(sPath))
                {
                }
                return true;
            }
            catch (Exception SQLError)
            {
                Log.Info("SQLite Ex.", SQLError.Message);
                return false;
            }
        }
    }
}

*-*-*-*-*-*-*-*-*-*-*-
** Button com a chamanda a classe DBRepositorio **
 try
 {
  DBRepositorio repo = new DBRepositorio();
    if (repo.CreateDB())
    {
        repo.CreateTable();
        Toast.MakeText(this, "Tabela criada", ToastLength.Long).Show();
    }
 }
 catch (Exception Error)
 {
   Toast.MakeText(this, Error.Message, ToastLength.Long).Show();
 }    

Att.,
Alexandre
  • What’s the matter?

  • Good morning Virgil, thanks for the return. I put in bold the point where the problem occurs and what the problem is. Thanks for your attention.

  • Virgil, I could not put the correct text in bold. As it is an EF reference, I believe it was read as some HTML tag of the site. So the bold section should be considered as: </br> Conn.Createtable<User_mobilemap>();</br>Thanks!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.