C
C#2mo ago
{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 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'.
32 Replies
{sus}
{sus}OP2mo ago
The reason I am doing this is there are "global" Items that can be utilized by anyone, but some items belong to specific people. These items are only accessible to them but not others.
Insire
Insire2mo ago
your OwnerId should be NULL, not 0
{sus}
{sus}OP2mo ago
Can it be zero?
Insire
Insire2mo ago
not with a int? OwnerId the default value for that is NULL
{sus}
{sus}OP2mo ago
Hmmmm Let me try
Insire
Insire2mo ago
which is what is used for relation ships by your database provider
{sus}
{sus}OP2mo ago
No go I still get the error and the migration still puts 0 as a default value
{sus}
{sus}OP2mo ago
No description
Insire
Insire2mo ago
well, thats what you specified via attribute 0 in a nullable column thats a foreignkey means, that there needs to be an entry on the other side of that relationship which has the primary key value of 0
{sus}
{sus}OP2mo ago
I already put null as the default value
{sus}
{sus}OP2mo ago
No description
Insire
Insire2mo ago
why do you even set that, just remove the attribute
{sus}
{sus}OP2mo ago
i just updated the value from 0 to null but lemme try removing it Same problem Hmmmm
Insire
Insire2mo ago
then idk. probably something else is wrong aswell this should work
cathei
cathei2mo ago
Do you have Id column in Owner?
{sus}
{sus}OP2mo ago
Yes. 100% positive I also checked the migrations generated If it helps, the seed data with 0 OwnerId is my only problem All the other seed data that has an ownerId of > 0 is fine
cathei
cathei2mo ago
Paste the code of your Owner class?
{sus}
{sus}OP2mo ago
No description
Insire
Insire2mo ago
if just your seed data is wrong, then correct your seed data and set the ownerid to NULL in there
{sus}
{sus}OP2mo ago
That's actually my last result but I want to solve this on code level first if possible
Insire
Insire2mo ago
this is the code lvl
{sus}
{sus}OP2mo ago
If and when someone decides to regenerate the migrations from scratch, this problem won't exist (if I solve it) Migrations are generated from the model code on a code first approach So yeah
cathei
cathei2mo ago
Oh you are saying you putting 0 as seed
{sus}
{sus}OP2mo ago
Yes.
cathei
cathei2mo ago
Why not null?
{sus}
{sus}OP2mo ago
That's also an option, but I'd rather have a place holder value for "global" items
cathei
cathei2mo ago
As a seed
{sus}
{sus}OP2mo ago
instead of null, for sanity purposes
cathei
cathei2mo ago
0 is valid ID So it will try to find a Owner with ID 0
{sus}
{sus}OP2mo ago
OHH
cathei
cathei2mo ago
And validation fails
{sus}
{sus}OP2mo ago
THAT MAKES PERFECT SENSE HOLD ON
The exception 'The seed entity for entity type 'Owner' cannot be added because a non-zero value is required for property 'Id'. Consider providing a negative value to avoid collisions with non-seed data.
The exception 'The seed entity for entity type 'Owner' cannot be added because a non-zero value is required for property 'Id'. Consider providing a negative value to avoid collisions with non-seed data.
lol Quirky let me try a different approach
Want results from more Discord servers?
Add your server