即使使用空外键MVC EF,也无法在没有查找记录的主表中插入记录

时间:2016-09-01 22:39:31

标签: asp.net-mvc entity-framework foreign-keys data-annotations nullable

这是我的两个班级。首先填充SellPrice个记录,然后使用SellPrice记录从JSON请求插入的BuyPrice记录中的信息。每条SellPrice条记录都有BuyPrice条记录。

我希望能够首先插入SellPrice条记录,但即使我已将SellPrice外键设置为可空,它仍然不会让我在没有查找的情况下保存SellPrice条记录记录在BuyPrice

我希望能够每隔一秒左右不断删除和更新BuyPrice JSON数据获取周期内的所有BuyPrice条记录。

我对此非常陌生,希望能够使用数据注释而不是流利的方法来实现这一目标。

非常感谢。这让我疯了!

public class SellPrice
{
    [Key]
    public int Id { get; set; }

    public DateTime EventDateTime { get; set; }
    public string EventId { get; set; }
    public String EventName { get; set; }
    public string MarketId { get; set; }
    public String MarketName { get; set; }
    public String SelectionId { get; set; }
    public String SelectionName { get; set; }
    public String SellerName { get; set; }
    public String SellPrice { get; set; }

    public BuyPrice BuyPrice { get; set; }
    public int? BuyPriceId { get; set; }
}

public class BuyPrice
{
    [Key]
    public int Id { get; set; }

    public string MarketId { get; set; }
    public string SelectionId { get; set; }
    public float BuyPrice { get; set; }
}

注意:我更改了Model的名称以便更容易理解关系,因此错误消息指的是实际结构。原理是相同的 - 如何能够插入没有相应父记录的子记录。

enter image description here

0 个答案:

没有答案