Entity Framework Core Mapping Multiple objects to an object in one to many

ef-code-first entity-framework-core object-object-mapping

Question

I am trying to work out the best way to handle a Note object that is linked to multiple objects i.e. Contacts, Quotes. These objects can have many Notes, A Quote has many Notes.

public class Note
{
    public int Id { get; set; }
    public string Title { get; set; }        
    public string NoteDetails { get; set; }
    public DateTime CreatedDate { get; set; }

    public int? ContactId { get; set; }
    public int? QuoteId { get; set; }

    public Contact NoteContact { get; set; }
    public Quote NoteQuote { get; set; }
}

public class Contact
{
    public int Id { get; set; }
    public string Name { get; set; }        
    public string Address { get; set; }

    public ICollection<Note> Notes { get; set; }
}

public class Quote    {
    public int Id { get; set; }
    public string Title { get; set; }        
    public string Description { get; set; }

    public ICollection<Note> Notes { get; set; }
}

Is it just a case of adding foreign keys in the Notes to Contacts, Quotes etc.? Then virtual collections of Notes in the Contact, Quotes and Invoices object?

I hope I make sense in what I am trying to achieve.

Thanks

1
1
3/21/2018 3:00:25 PM

Popular Answer

Having a model like this (maybe is not exactly thw way you want, but you get the point):

public class Note
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string NoteDetails { get; set; }
    public DateTime CreatedDate { get; set; }


    public Quote Quote { get; set; }
    public Contact Contact { get; set; }
}

public class Contact
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }

    public ICollection<Note> Notes { get; set; }
}

public class Quote
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }

    public ICollection<Note> Notes { get; set; }
}

Will create this migration:

migrationBuilder.CreateTable(
    name: "Contacts",
    columns: table => new
    {
        Id = table.Column<int>(nullable: false)
            .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
        Address = table.Column<string>(nullable: true),
        Name = table.Column<string>(nullable: true)
    },
    constraints: table =>
    {
        table.PrimaryKey("PK_Contacts", x => x.Id);
    });

migrationBuilder.CreateTable(
    name: "Quotes",
    columns: table => new
    {
        Id = table.Column<int>(nullable: false)
            .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
        Description = table.Column<string>(nullable: true),
        Title = table.Column<string>(nullable: true)
    },
    constraints: table =>
    {
        table.PrimaryKey("PK_Quotes", x => x.Id);
    });

migrationBuilder.CreateTable(
    name: "Notes",
    columns: table => new
    {
        Id = table.Column<int>(nullable: false)
            .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
        ContactId = table.Column<int>(nullable: true),
        CreatedDate = table.Column<DateTime>(nullable: false),
        NoteDetails = table.Column<string>(nullable: true),
        QuoteId = table.Column<int>(nullable: true),
        Title = table.Column<string>(nullable: true)
    },
    constraints: table =>
    {
        table.PrimaryKey("PK_Notes", x => x.Id);
        table.ForeignKey(
            name: "FK_Notes_Contacts_ContactId",
            column: x => x.ContactId,
            principalTable: "Contacts",
            principalColumn: "Id",
            onDelete: ReferentialAction.Restrict);
        table.ForeignKey(
            name: "FK_Notes_Quotes_QuoteId",
            column: x => x.QuoteId,
            principalTable: "Quotes",
            principalColumn: "Id",
            onDelete: ReferentialAction.Restrict);
    });

And finnaly, doing something like this:

var quotes = _db.Quotes.Include(q => q.Notes).ThenInclude(n => n.Contact).ToList();

you should have a list of Quotes with inner objects (Notes and Contacts) in it, and you can use the navigation properties.

0
3/22/2018 6:48:48 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