我正在使用ASP.NET MVC 4和Entity Framework5。我使用了模型优先方法来生成数据库。在我的应用程序中,我有一个正在运行的日志表和一个鞋子表。用户可以将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)
返回空值并将le.Shoe
设置为null
并将le.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
模型生成的日志条目类
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; }
}
最佳答案
如果您想要0到很多,那么您的外键必须可以为空,即:
public int? ShoeId { get; set; }
就像现在一样,您已经告诉EF
ShoeId
不能为null,因此您只有1比1。编辑
抱歉,我看错了课,但我会将其留给可能需要它的其他人。但是,这里的情况还是差不多。发生的情况是您没有遵循EF约定进行FK命名(
ShoeShoeId
)。对于Shoe
这样的虚拟,EF会查找属性ShoeId
。如果找到一个,那就是FK字段,否则,EF会在数据库中自动为FK添加一个Shoe_ShoeId
字段。此自动添加的字段将不可为空,这就是这里发生的情况。如果您坚持认为自己的FK是
ShoeShoeId
,则只需明确告诉EF这就是Shoe
的FK:[ForeignKey("Shoe")]
public int? ShoeShoeId { get; set; }
关于c# - Entity Framework 5-0..1与多关系无法保存新实体,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15623273/