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

Did you find this page helpful?