Consider the following example model:
Attempt 1:
Person
to User
on the model designer. Problem:
User
table has a Person_id
column with no unique constraint. That is, it's not a 1x1 relationship as many Users can reference the same Person. There must be something wrong here Attempt 2:
Referential Constraint
button on the properties window to edit the association Principal Key
to be Id and the Dependent Property
to be also Id (I've read somewhere that I should use the same key for both types) Problem:
I run this code:
using (var context = new Locadora())
{
User user = new User ();
user.PasswordHash = "hash";
user.Pessoa = new Person();
user.Pessoa.Nome = "André";
context.Usuários.AddObject(user);
context.SaveChanges();
}
A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'Id'
So I am out of choices now. I don't know how to implement a 1x1 neither a 1x0..1 relationship in Entity Framework.
How do I do that?
The first attempt can't work because current EF version doesn't support Unique keys at all. Second approach doesn't work because you dependent entity can't have autogenerated key = StoreGeneratedPattern
must be set to None
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.