0

I have a DbContext which I via the developer command prompt and creating a migrations schema turn in to my database. But if you look at the product object I have a dictionary object named Parts. That property does not get added to the Product table when the database is updated in the command prompt. I don't even know if it is possible what I am trying to do.

I want to add a table in the database named Parts and then add a foreign key to the Product table which connects the Parts dictionary object in the Product table, and the the new Parts table. Is this possible with Entity Framework Core?

public class ShoppingDbContext : IdentityDbContext<User>
{
    public ShoppingDbContext(DbContextOptions options) : base(options)
    {
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        base.OnConfiguring(optionsBuilder);
    }

    public DbSet<Product> Products { get; set; }
    public DbSet<Order> Orders { get; set; }
}

public class Product 
{
    public int ProductId { get; set; }
    public string ProductName { get; set; }
    public double Price { get; set; }
    public int CategoryId { get; set; }
    Dictionary<string, Part> Parts { get; set; }
}

4 Answers 4

1

EF Core can't currently map a dictionary property directly. If you want to create an association between Products and Parts, then define each of them as an entity. You can then create navigation properties between them--a reference from Part to the Product which it belongs, and a collection of Parts on Product. For example:

public class Product 
{
    public int ProductId { get; set; }
    public string ProductName { get; set; }
    public double Price { get; set; }
    public int CategoryId { get; set; }
    public ICollection<Part> Parts { get; set; }
}

public class Part
{
    public int PartId { get; set; }
    public int ProductId { get; set; }
    public Product Product { get; set;}
}

Part also defines a property ProductId that acts as the FK to the Product entity. You don't need to add that property--EF will simulate it for you if you don't want it, but usually it is easier to deal with entities if the FK is mapped to a property.

Sign up to request clarification or add additional context in comments.

Comments

0

Relationships are tracked through object references instead of foreign key properties. This type of association is called an independent association.

More Details Here:

https://msdn.microsoft.com/en-us/data/jj713564.aspx

Sample code:

public partial class Product
{
    public Product()
    {
        this.Parts = new HashSet<Part>();
    }

    public int ProductId { get; set; }
    public string ProductName { get; set; }
    public double Price { get; set; }
    public int CategoryId { get; set; }

    public virtual ICollection<Part> Parts { get; set; }

}

4 Comments

Can you add that paragrapgh you took away
That helped me @LJ
This dosent wotrk for me
@L J Independent associations are part of the old EF stack. That concept and the documentation you link does not apply to EF Core. In EF Core, all associations are tracked through FKs, although that FK can be in "shadow state" which makes it somewhat like an independent association. Navigation properties act as a view over the FK.
0

Basically like what Arthur said, EF Core does not support it yet.

However, another way is to create a composite table should you want to or if it's viable for your use.

Here's a simple example:

            // -------------- Defining BrandsOfCategories Entity --------------- //

        modelBuilder.Entity<BrandCategory>()
            .HasKey(input => new { input.BrandId, input.CatId })
            .HasName("BrandsOfCategories_CompositeKey");

        modelBuilder.Entity<BrandCategory>()
            .Property(input => input.DeletedAt)
            .IsRequired(false);

        // -------------- Defining BrandsOfCategories Entity --------------- //

   public class BrandCategory
{
    public int CatId { get; set; }
    public int BrandId { get; set; }
    public DateTime? DeletedAt { get; set; }
    public Category Category { get; set; }
    public Brands Brand { get; set; }
}

The DeletedAt is optional of course. This handles M-M Relationships.

Comments

0

I had the same issue, I resolved it by removing the keyword virtual on the navigation properties and with in the ApplicatinDbContext

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.