EF Core设置列添加子属性值

时间:2017-12-02 17:38:13

标签: entity-framework asp.net-core .net-core entity-framework-core ef-core-2.0

我有一个链接和内容,新闻实体。

**Link**: Id, Url, RowId, Discriminator
**Content**: Id, Text, Link
**News**: Id, Date, Text, Link

当我添加新内容或新闻时,我想自动将RowId设置为插入的Id。 需要通过Link.Url

来纠正解析实体

因此,如果我有Link.Discriminator和Link.RowId,我可以确定我需要哪个实体(内容或新闻)。

帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

据我所知,这应该是你所追求的。

public void AddContent(string Text, string Link)
{
    var content = new Content {
        Text = Text,
        Link = Link
        };

    //The id is collected when you add the Entity to the context
    dbContext.SaveChanges();

    dbContext.Link.Add(new Link {
        Url = <whatever>,
        RowId = content.Id,
        Discriminator = "Content"
    });

    dbContext.SaveChanges();
}
相关问题