Mapping a Listlong in LINQ to SQL - Windows Phone 7.1

2

Problem

I'm starting an application for Windows Phone 7.1, and I'm implementing the database, but I came across a situation that I did not find any similar situation to try to solve, and I also did not find anything to help me with the documentation. Microsoft.

The situation is as follows:

I have class similar to this:

public class Setup : INotifyPropertyChanged, INotifyPropertyChanging
{
    private long _id;
    private string _descricao;
    private IList<long> _tempos;

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

    [Column]
    public virtual string Descricao
    {
        get { return _descricao; }
        set
        {
            if (value != _descricao)
            {
                _descricao = value;
                NotifyPropertyChanged("Nome");
            }
        }
    }

    // como eu faria para mapear corretamente esse campo
    [Column] 
    public virtual IList<long> Tempos
    {
        get
        {
            if (_tempos == null)
                _tempos = new List<long>();
            return _tempos;
        }
        set
        {
            if (value != _tempos)
            {
                NotifyPropertyChanging("");
                _tempos = value;
                NotifyPropertyChanged("Tempos");
            }
        }
    }

    // Version column aids update performance.
    [Column(IsVersion = true)]
    private Binary _version;

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public event PropertyChangingEventHandler PropertyChanging;
    private void NotifyPropertyChanging(string propertyName)
    {
        if (PropertyChanging != null)
        {
            PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
        }
    }
}

Question

In the case, how would I correctly map the IList<long> Tempos , since it is not a simple field, it is a list of long , or would have to create a class (Time, for example) and treat as a 1 x N relationship? If this is how it would be? If possible with examples.

    
asked by anonymous 17.10.2014 / 04:38

2 answers

1

I was able to solve the problem (with the @CiganoMorrisonMendez tips, but with some important modifications to the collections), as follows:

I created a new entity to represent the times, called Tempo :

[Table]
public class Tempo : INotifyPropertyChanged, INotifyPropertyChanging
{
    private long _id;
    [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
    public virtual long Id
    {
        get { return _id; }
        set
        {
            if (value != _id)
            {
                NotifyPropertyChanging("Id");
                _id = value;
                NotifyPropertyChanged("Id");
            }
        }
    }

    private long _minutos;
    [Column]
    public virtual long Minutos
    {
        get { return _minutos; }
        set
        {
            if (value != _minutos)
            {
                NotifyPropertyChanging("Minutos");
                _minutos = value;
                NotifyPropertyChanged("Minutos");
            }
        }
    }

    [Column]
    public long SetupID;
    private EntityRef<Setup> _setup;
    [Association(Storage = "_setup", ThisKey = "SetupID")]
    public virtual Setup Setup
    {
        get { return _setup.Entity; }
        set
        {
            if (value != _setup.Entity)
            {
                NotifyPropertyChanging("Setup");
                _setup.Entity = value;
                NotifyPropertyChanged("Setup");
            }
        }
    }

    // ...
}

And I modified the mapping of the attribute Tempos from class Setup , to the following:

[Table]
public class Setup : INotifyPropertyChanged, INotifyPropertyChanging
{        
    // ...

    private EntitySet<Tempo> _tempos;
    [Association(Storage = "_tempos", OtherKey = "Id")]
    public virtual EntitySet<Tempo> Tempos
    {
        get
        {
            if (_tempos == null)
                _tempos = new EntitySet<Tempo>();
            return _tempos;
        }
        set
        {
            if (value != Tempos)
            {
                NotifyPropertyChanging("Tempos");
                Tempos.Assign(value);
                NotifyPropertyChanged("Tempos");
            }
        }
    }

    // ...
}

The original problem was that IList<long> were not entities known for LINQ to SQL , so he did not know how to manage those attributes, in this case the @CiganoMorrisonMendez solution was to create an entity to represent Tempo (In the case of the table that would save the times of Setup ), and represents it as a ICollection<Tempo> in Setup , in the case of ICollection , also ended up not being recognized by LINQ to SQL , which sent me the following Exception when trying to create the database: Unable to determine SQL type for 'System.Collections.Generic.ICollection'1 , then by searching I got to following Microsoft "obscure" documentation , which saved my day. Where it explains and demonstrates how to represent / map cardinality relationships in LINQ to SQL where:

  • EntitySet: That is used as object / list to represent cardinality relationships ( 1 X N and N X N );
  • EntityRef: which is used as an object to represent simple association between two entities;
18.10.2014 / 06:58
1

In case, how would I correctly map the IList<long> Tempos field, since it is not a simple field, is a list of long, or would have to create a class ( Tempo , for example) and treat as a relationship 1 x N?

Exactly.

Any list or structure of data that suggests cardinality N must be declared as another entity, related to the entity in question.

And how would I deal with this cardinality? What annotation would I use?

More or less like this:

public class Entidade
{
    [Key]
    public int EntidadeId { get; set; }

    public virtual ICollection<Tempo> Tempos { get; set; }
}
    
17.10.2014 / 05:33