C# 实体框架5-0..1对多关系可以';不保存新实体
我正在使用ASP.NETMVC4和EntityFramework5。我使用模型优先的方法来生成数据库。在我的应用程序中,我有一个跑步日志表和一个鞋表。用户可以有0或1双鞋与跑步日志条目关联。这看起来像是一个0..1对多的关系,这就是我在模型中设置它的方式。当我执行C# 实体框架5-0..1对多关系可以';不保存新实体,c#,asp.net,sql,asp.net-mvc,entity-framework,C#,Asp.net,Sql,Asp.net Mvc,Entity Framework,我正在使用ASP.NETMVC4和EntityFramework5。我使用模型优先的方法来生成数据库。在我的应用程序中,我有一个跑步日志表和一个鞋表。用户可以有0或1双鞋与跑步日志条目关联。这看起来像是一个0..1对多的关系,这就是我在模型中设置它的方式。当我执行context.SaveChanges()添加一个新条目而没有任何与之关联的鞋子时,我会遇到以下错误: INSERT语句与外键约束“FK\u ShoeLogEntry\”冲突 只要我挑选一双鞋子作为日志条目,它就可以正常工作。那么,如何
context.SaveChanges()
添加一个新条目而没有任何与之关联的鞋子时,我会遇到以下错误:
INSERT语句与外键约束“FK\u 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
主键设置
-- 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; }
}
公共部分类日志条目
{
public int LogId{get;set;}
公共字符串ActivityName{get;set;}
public System.DateTime StartTime{get;set;}
公共字符串时区{get;set;}
公共整数持续时间{get;set;}
公共十进制距离{get;set;}
公共可空卡路里{get;set;}
公共字符串说明{get;set;}
公共字符串标记{get;set;}
public int userid{get;set;}
公共可为空的鞋店ID{get;set;}
public int activitypesid{get;set;}
公共虚拟用户用户{get;set;}
公共虚拟鞋{get;set;}
公共虚拟活动类型活动类型{get;set;}
}
模型生成的鞋类
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; }
}
公共部分类鞋
{
公众鞋()
{
该点距离=0m;
this.LogEntries=new HashSet();
}
公共int ShoeId{get;set;}
公共字符串ShoeName{get;set;}
公共十进制ShoeDistance{get;set;}
公共int ShoeUserId{get;set;}
公共字符串ShoeBrand{get;set;}
公共字符串ShoeModel{get;set;}
public System.DateTime PurchaseDate{get;set;}
public int userid{get;set;}
公共虚拟用户用户{get;set;}
公共虚拟ICollection日志项{get;set;}
}
如果希望0到多,则外键必须可以为空,即:
public int? ShoeId { get; set; }
现在,您已经告诉EF,ShoeId
不能为空,因此只有1对多
编辑
对不起,我看错了课程,但我会把它留给其他可能需要它的人。然而,这里的情况仍然差不多。所发生的情况是,您没有遵循EF的FK命名约定(ShoeShoeId
)。对于类似于Shoe
,EF会查找属性ShoeId
。如果它找到一个,那将是FK字段,如果没有,EF将在数据库中自动为FK添加一个Shoe_ShoeId
字段。这个自动添加的字段将不可为空,这就是这里发生的情况
如果您坚持您的FK为鞋鞋ID,只需明确告诉EF这是鞋的FK:
[ForeignKey("Shoe")]
public int? ShoeShoeId { get; set; }
它在
LogEntry
类上可以为空。在Shoe
上它不应该为空。谢谢你,它工作得很好!有一件事让我大吃一惊,那就是支持表格的模型。我没有将该模型中的ShoeId
设置为Nullable
,因此在提交时为其分配了一个0值,而不是null
,EF似乎不喜欢这样。再次感谢你,我感谢你的帮助!您的LogEntry
表中ShoeShoeId
的定义是什么?(在SQL中)