P
P
ProX_Alex2018-07-31 10:46:28
C++ / C#
ProX_Alex, 2018-07-31 10:46:28

How to link a table to itself and add additional data to EF?

We have the essence of the product (product). Additional modules (or may not be inserted) can be inserted into this product, which in themselves are products (products). Also, the product itself can be inserted into another product:

public class Product : INotifyPropertyChanged
    {
        public int ProductId { get; set; }

        /// <summary>
        /// В какие модули можно вставлять
        /// </summary>
        public virtual ObservableCollection<Product> ForProducts { get; set; }


        /// <summary>
        ///  Какие модули можно вставлять в меня
        /// </summary>
        public virtual ObservableCollection<Product> AddModules {get; set;}

.....другие поля

set up relationships via FluentApi
protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Product>().HasMany(x => x.AddModules).WithMany(y => y.ForProducts)
                .Map(x => x.ToTable("ProductToProduct"));
        }

Everything is great, everything works. But you need to add additional data to the link table, for example
//сколько штук данного продукта(изделия) можно вставить в другой продукт(изделие)
public int Count {get;set;}

// единица измерения (штуки, комплекты и тд.)
public string Unit{get;set;}

...

I want to get the many-to-many relation of the Product entity to itself, through an additional table where additional columns can be added.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question