19 Kasım 2016 Cumartesi
Entitiy Framework 'The Name value should be a comma separated list of foreign key property names' Error
'The ForeignKeyAttribute on property 'Owner' on type 'MyProject.Models.Comment' is not valid. The foreign key name 'OwnerId' was not found on the dependent type 'MyProject.Models.Comment'. The Name value should be a comma separated list of foreign key property names.'
I have encountered that error above while i was uploading my database using package manager console.(update-database process on consele)
My problem is about with two classes named User and Comment.There is one to many relation between them.Each user can have comments more than one and every comment belongs to an owner(user).Below are my classes:
public class Comment : BaseObject
{
[ForeignKey("OwnerId")]
public virtual User Owner { get; set; }
public Guid OwnerId;
public virtual ICollection<Comment> Replies{ get; set; }
}
public class User : BaseObject
{
public virtual ICollection<Comment> Comments { get; set; }
}
Solution 1 :
In such cases be sure that your foreign key is available as primitive(int,guid,etc) and sign it using data annotation that is [ForeignKey] on related object property.In this sample you have to write like that :
[ForeignKey("OwnerId")]
public virtual User Owner { get; set; }
public Guid OwnerId;
! Don't forget that which key is going to be used for Owner object.
Solution 2 :
And also you need to declare your primitive key carrier as property.Not like above.
public Guid OwnerId{get;set;}
Kaydol:
Kayıt Yorumları (Atom)
Hiç yorum yok:
Yorum Gönder