实体框架核心将多个对象映射到一对多的对象

时间:2018-03-21 15:00:25

标签: ef-code-first entity-framework-core object-object-mapping

我正在尝试找出处理链接到多个对象的Note对象的最佳方法,即Contacts,Quotes。这些对象可以有很多Notes,A Quote有很多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; }
}

仅仅是将Notes中的外键添加到联系人,报价等中的情况?然后是联系人,报价单和发票对象中的Notes虚拟集合?

我希望我能够理解我想要实现的目标。

由于

1 个答案:

答案 0 :(得分:0)

拥有这样的模型(可能不是你想要的那种方式,但你明白了):

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; }
}

将创建此迁移:

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);
    });

最终,做这样的事情:

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

你应该有一个包含内部对象(注释和联系人)的报价列表,你可以使用导航属性。