【问题标题】:Entity Framework 5 - 0..1 to Many Relationship can't save new entity实体框架 5 - 0..1 到许多关系无法保存新实体
【发布时间】:2013-03-15 10:25:00
【问题描述】:

我正在使用 ASP.NET MVC 4 和 Entity Framework 5。我使用模型优先的方法来生成数据库。在我的应用程序中,我有一个运行日志表和一个鞋子表。用户可以拥有与跑步日志条目相关联的 0 或 1 双鞋。所以这似乎是一个 0..1 对多的关系,这就是我在模型中设置它的方式。当我 context.SaveChanges() 添加一个没有任何关联鞋子的新条目时,我收到此错误:

The INSERT statement conflicted with the FOREIGN KEY constraint \"FK_ShoeLogEntry\".

只要我为日志条目挑选一双鞋,它就可以正常工作。那么如何正确设置关系,以便日志条目可以为鞋子提供空值?我在这里粘贴了下面的代码:

我用来添加新条目的代码

le.ActivityType = ctx.ActivityTypes.Find(le.ActivityTypesId);
le.User = ctx.Users.Find(le.UserUserId);
le.Shoe = ctx.Shoes.Find(le.ShoeShoeId); //ShoeShoeId is null if nothing picked
ctx.LogEntries.Add(le);
ctx.SaveChanges();

我尝试检查 ctx.Shoes.Find(le.ShoeShoeId) 是否返回 null 并将 le.Shoe 设置为 nullle.ShoeShoeId 设置为 null-1 但这没有用。

我试图在下面粘贴相关代码,但这对我来说很新。所以如果有必要我可以添加更多。非常感谢任何帮助!

外键设置

-- Creating foreign key on [ShoeShoeId] in table 'LogEntries'
ALTER TABLE [dbo].[LogEntries]
ADD CONSTRAINT [FK_ShoeLogEntry]
FOREIGN KEY ([ShoeShoeId])
REFERENCES [dbo].[Shoes]
    ([ShoeId])
ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ShoeLogEntry'
CREATE INDEX [IX_FK_ShoeLogEntry]
ON [dbo].[LogEntries]
   ([ShoeShoeId]);
GO

主键设置

-- Creating primary key on [ShoeId] in table 'Shoes'
ALTER TABLE [dbo].[Shoes]
ADD CONSTRAINT [PK_Shoes]
   PRIMARY KEY CLUSTERED ([ShoeId] ASC);
GO

-- Creating primary key on [LogId] in table 'LogEntries'
ALTER TABLE [dbo].[LogEntries]
ADD CONSTRAINT [PK_LogEntries]
   PRIMARY KEY CLUSTERED ([LogId] ASC);
GO

模型生成的Log Entry类

public partial class LogEntry
{
    public int LogId { get; set; }
    public string ActivityName { get; set; }
    public System.DateTime StartTime { get; set; }
    public string TimeZone { get; set; }
    public int Duration { get; set; }
    public decimal Distance { get; set; }
    public Nullable<int> Calories { get; set; }
    public string Description { get; set; }
    public string Tags { get; set; }
    public int UserUserId { get; set; }
    public Nullable<int> ShoeShoeId { get; set; }
    public int ActivityTypesId { get; set; }

    public virtual User User { get; set; }
    public virtual Shoe Shoe { get; set; }
    public virtual ActivityTypes ActivityType { get; set; }
}

模型生成的鞋类

public partial class Shoe
{
    public Shoe()
    {
        this.ShoeDistance = 0m;
        this.LogEntries = new HashSet<LogEntry>();
    }

    public int ShoeId { get; set; }
    public string ShoeName { get; set; }
    public decimal ShoeDistance { get; set; }
    public int ShoeUserId { get; set; }
    public string ShoeBrand { get; set; }
    public string ShoeModel { get; set; }
    public System.DateTime PurchaseDate { get; set; }
    public int UserUserId { get; set; }

    public virtual User User { get; set; }
    public virtual ICollection<LogEntry> LogEntries { get; set; }
}

【问题讨论】:

  • ShoeShoeId 在您的LogEntry 表上的定义是什么? (在 SQL 中)

标签: c# asp.net sql asp.net-mvc entity-framework


【解决方案1】:

如果你想要 0 到多个,那么你的外键必须是可空的,即:

public int? ShoeId { get; set; }

正如你现在所拥有的,你已经告诉 EF ShoeId 不能为空,所以你只有一对多。

编辑

抱歉,我看错了课程,但我会把它留给其他可能需要它的人。但是,这里的情况仍然几乎相同。发生的事情是您没有遵循 FK 命名的 EF 约定 (ShoeShoeId)。对于像Shoe 这样的虚拟,EF 会查找属性ShoeId。如果找到一个,那将是 FK 字段,如果没有,EF 将在您的数据库中自动为 FK添加一个 Shoe_ShoeId 字段。这个自动添加的字段将是不可为空的,这就是这里发生的事情。

如果你坚持你的 FK 是 ShoeShoeId,只需明确告诉 EF 这是Shoe 的 FK:

[ForeignKey("Shoe")]
public int? ShoeShoeId { get; set; }

【讨论】:

  • 它在 LogEntry 类上可以为空。它不应该在 Shoe 上为空。
  • 感谢您的完美工作!让我绊倒的一件事是支持表格的模型。我没有将该模型中的ShoeId 设置为Nullable&lt;Int32&gt;,因此它在提交时被分配了一个0 值而不是null,而EF 似乎并不喜欢这样。再次感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多