C
C#2w 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}2w 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
Insire2w ago
your OwnerId should be NULL, not 0
{sus}
{sus}2w ago
Can it be zero?
Insire
Insire2w ago
not with a int? OwnerId the default value for that is NULL
{sus}
{sus}2w ago
Hmmmm Let me try
Insire
Insire2w ago
which is what is used for relation ships by your database provider
{sus}
{sus}2w ago
No go I still get the error and the migration still puts 0 as a default value
{sus}
{sus}2w ago
No description
Insire
Insire2w 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}2w ago
I already put null as the default value
{sus}
{sus}2w ago
No description
Insire
Insire2w ago
why do you even set that, just remove the attribute
{sus}
{sus}2w ago
i just updated the value from 0 to null but lemme try removing it Same problem Hmmmm
Insire
Insire2w ago
then idk. probably something else is wrong aswell this should work
cathei
cathei2w ago
Do you have Id column in Owner?
{sus}
{sus}2w 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
cathei2w ago
Paste the code of your Owner class?
{sus}
{sus}2w ago
No description
Insire
Insire2w ago
if just your seed data is wrong, then correct your seed data and set the ownerid to NULL in there
{sus}
{sus}2w ago
That's actually my last result but I want to solve this on code level first if possible
Insire
Insire2w ago
this is the code lvl
{sus}
{sus}2w 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
cathei2w ago
Oh you are saying you putting 0 as seed
{sus}
{sus}2w ago
Yes.
cathei
cathei2w ago
Why not null?
{sus}
{sus}2w ago
That's also an option, but I'd rather have a place holder value for "global" items
cathei
cathei2w ago
As a seed
{sus}
{sus}2w ago
instead of null, for sanity purposes
cathei
cathei2w ago
0 is valid ID So it will try to find a Owner with ID 0
{sus}
{sus}2w ago
OHH
cathei
cathei2w ago
And validation fails
{sus}
{sus}2w 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