Entity Framework 5 - 0..1 to Many Relationship can't save new entity
asp.net, asp.net-mvc, c#, entity-framework, sql
Solution
If you want 0 to many, then your foreign key must be nullable, i.e.:
public int? ShoeId { get; set; }
As you have it now, you've told EF that `ShoeId` can't be null, so you have only a 1 to many.
EDIT
Sorry, I was looking at the wrong class, but I'll leave that for anyone else who may need it. However, the situation is still pretty much the same here. What's happening is that you're not following EF conventions for FK naming (`ShoeShoeId`). For a virtual like `Shoe`, EF looks for a property `ShoeId`. If it finds one, that will be the FK field, if not, EF will add a `Shoe_ShoeId` field in your database automatically for the FK. This auto-added field will be non-nullable, which is what's happening here.
If you insist on your FK being `ShoeShoeId`, just tell EF explicitly that this is the FK for `Shoe`:
[ForeignKey("Shoe")]
public int? ShoeShoeId { get; set; }
Problem
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 `context.SaveChanges()` to add a new entry without any shoes associated with it I get this error: `The INSERT statement conflicted with the FOREIGN KEY constraint \"FK_ShoeLogEntry\".` 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 ``` 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(); ``` I have tried checking for `ctx.Shoes.Find(le.ShoeShoeId)` returning null and setting `le.Shoe` to `null` and `le.ShoeShoeId` to `null` and `-1` 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 ``` -- 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 ``` Primary Key setup ``` -- 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 class generated by model ``` 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; } } ``` Shoe class generated by model ``` 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; } } ```