Establishing relationships while aggregating more tables into one

c# entity-framework entity-framework-core

Question

I have a problem I need to solve - I'm developing a data model for new application. I'm using code first .net core.

In my app user can create multiple new items, with every item having little different attributes. I need to 'aggregate' these items into one table so I can for example add commentaries on each item.

Therefore there is always 1:1 relationship between Item1 and AggItem and Item2 and AggItem.

Table AggItem does exactly that. There should be composite primary key consisting of

  1. ItemId which is primary key in Item1 or Item2 tables
  2. ItemType which is string that will distinguish between items

I've already tried a lot of things, but I can't really get this working. I really just wanna have the data model up and running at this point.

Any help appreciated!

Data in the AggItem table will look like this:

ID  ItemId  ItemType    Description
1   1       "item1"     "First item from table Item1"
2   2       "item1"     "Second item from table Item1"
3   1       "item2"     "First item from table Item2"
4   2       "item2"     "Second item from table Item2"

App Context:

protected override void OnModelCreating(ModelBuilder builder)
{
    builder.Entity<AggItem>().HasKey(table => new {
        table.ItemId,
        table.ItemType
    });
}

Domain class models:

public class Item1
{
    public int Id { get; set; }
    public string Description { get; set; }
    public string UsedByCompany { get; set; }
    public string Level { get; set; }
    public int AggItemId { get; set; }
    public virtual AggItem AggItem { get; set; }
}

public class Item2
{
    public int Id { get; set; }
    public string Description { get; set; }
    public string Confidentiality { get; set; }
    public string Quantity { get; set; }
    public int AggItemId { get; set; }
    public virtual AggItem AggItem { get; set; }
}

public class AggItem
{

    public int Id { get; set; }
    public string Description { get; set; }
    public int ItemId { get; set; }
    public string ItemType { get; set; }

    public virtual Comments {get; set;}
    public virtual Item1 item1 { get; set; }
    public virtual Item2 item2 { get; set; }
}

Item1 create method:

[BindProperty]
public Item1 Item1 { get; set; }

public async Task<IActionResult> OnPostAsync()
{
    if (!ModelState.IsValid)
    {
        return Page();
    }

    var aggItem = new AggItem
    {
        item1 = Item1,
        Description = Item1.Description,
        ItemType = "Item1",
        ItemId = Item1.Id
    };

    _context.Add(new Item1
    {
        Description = Item1.Description,
        AggItem = aggItem

    });

    _context.item1.Add(Item1);
    await _context.SaveChangesAsync();
    return RedirectToPage("./Index");
}
1
0
2/25/2019 12:44:34 AM

Popular Answer

You should use OO inheritance model. Item1, Item2 shoud inherit from BaseItem. More info: https://docs.microsoft.com/en-us/ef/core/modeling/relational/inheritance

0
2/26/2019 10:04:59 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow