Entity Framework 5 - 0..1 to Many Relationship can't save new entity

bahadir498

New Member
I am using ASP.NET MVC 4 and Entity Framework 5. I used the model first approach for generating the database. In my application, I have a running log table and a shoes table. The user can have 0 or 1 pair of shoes associated with a running log entry. So that seems like a 0..1 to many relationship and that's how I set it up in the model. When I do \[code\]context.SaveChanges()\[/code\] to add a new entry without any shoes associated with it I get this error:\[code\]The INSERT statement conflicted with the FOREIGN KEY constraint \"FK_ShoeLogEntry\".\[/code\]As soon as I pick a pair of shoes for a log entry it works fine. So how do I set up the relationship properly so a log entry can have a null value for shoes? I have pasted in the code below here:Code I'm using to add a new entry\[code\]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 pickedctx.LogEntries.Add(le);ctx.SaveChanges();\[/code\]I have tried checking for \[code\]ctx.Shoes.Find(le.ShoeShoeId)\[/code\] returning null and setting \[code\]le.Shoe\[/code\] to \[code\]null\[/code\] and \[code\]le.ShoeShoeId\[/code\] to \[code\]null\[/code\] and \[code\]-1\[/code\] and that didn't work. I tried to paste below here relevant code, but this is pretty new to me. So I can add more if necessary. I really appreciate any help!Foreign Key Setup\[code\]-- 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\[/code\]Primary Key setup\[code\]-- 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\[/code\]Log Entry class generated by model\[code\]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; }}\[/code\]Shoe class generated by model\[code\]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; }}\[/code\]
 
Back
Top