© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•16mo ago•
53 replies
{sus}

Setting a Default Value for Migrations

I have a relationship between two models but the relationship is not required, i.e. the first model which "belongs to" the other model can exists on its own.

class Item {

  [DefaultValue(0)]
  public int? OwnerId {get;set;}
  
  public Owner Owner { get;set; }
}

class Owner {
  ...
}
class Item {

  [DefaultValue(0)]
  public int? OwnerId {get;set;}
  
  public Owner Owner { get;set; }
}

class Owner {
  ...
}


So in essence, class
Item
Item
can stand on its own but it can also belong to someone.

I am getting this when trying to run the database update command for the seed data which has set the OwnerId to 0

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Items_Owners_OwnerId". The conflict occurred in database "<DB>", table "dbo.Owners", column 'Id'.
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Items_Owners_OwnerId". The conflict occurred in database "<DB>", table "dbo.Owners", column 'Id'.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements
Next page

Similar Threads

Default struct with default value [Answered]
C#CC# / help
4y ago
❔ Appease "Property is uninitialized" without setting a default value? Possibly make it required?
C#CC# / help
3y ago
✅ abstract class - default value
C#CC# / help
3y ago