M
M
Maxim Isaev2017-01-20 15:00:07
WPF
Maxim Isaev, 2017-01-20 15:00:07

How to display from the database to the datagrid (WPF + Entity)?

Good day, the problem is that when I query the database, I get an exception, where did I make a mistake?
The Model contains
the Client

namespace WpfApplication2.Models
{
   [Table("Client")]
    public class Client
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        [Column("Id")]
        public int Id { get; set; }
        [ForeignKey("Id")]

        [Column("ClientName")]
        public string ClientName { get; set; }

        [Column("DateBirth")]
        public string DateBirth { get; set; }

        public ICollection<Phone> Phones { get; set; }

    }

}

phone
namespace WpfApplication2.Models
{
   [Table("Phone")]
    public class Phone
    {
       [Key]
       [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
       [Column("Id")]
        public int Id{ get; set; }
        [Column("Id_Client")]
        public int Id_Client { get; set; }
        [ForeignKey("Id_Client")]
        [Column("PhoneNumber")]
        public string PhoneNumber { get; set; }
        public Client Clients { get; set; }
    }
}

Context
namespace WpfApplication2.Models
{
    class ClientContext : DbContext
    {
        public ClientContext(): base("DefaultConnection")
        {
             
        }
        public DbSet<Client> Clients { get; set; }
        public DbSet<Phone> Phones { get; set; }
    }
}

And the call goes like this
private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var kk = db.Clients.ToList();
            var dd = db.Phones.ToList();
            personGrid.ItemsSource = kk;
            personGrid.ItemsSource = dd;


        }

Exception message
The property 'Id' cannot be configured as a navigation property. The property must be a valid entity type and the property should have a non-abstract getter and setter. For collection properties the type must implement ICollection where T is a valid entity type.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sanostee, 2017-01-21
@MaximIs

public class Phone
    {
      .....
       
        [ForeignKey("Id_Client")]
        public Client Clients { get; set; }
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question