This question already has answers here:
What does principal end of an association means in 1:1 relationship in Entity framework
(3 answers)
Closed 6 years ago.
public class Picture
{
[Key]
public int Id { get; set; }
public int NewsId { get; set; }
[ForeignKey("NewsId")]
public virtual News News { get; set; }
public int PostId { get; set; }
[ForeignKey("PostId")]
public virtual Post Post { get; set; }
}
public class News
{
public int Id { get; set; }
public virtual Picture Picture { get; set; }
}
public class Post
{
public int Id { get; set; }
public virtual Picture Picture { get; set; }
}
Exception:
Unable to determine the principal end of an association between the
types 'Project.BusinessObjects.Photo' and
'Project.BusinessObjects.Student'. The principal end of
this association must be explicitly configured using either the
relationship fluent API or data annotations.
Whats wrong?
I think you can solve the problem by creating new [key] data annotations above the other classes' keys. Like this:
public class Picture
{
[Key]
public int Id { get; set; }
[ForeignKey("News")]
public int NewsId { get; set; }
public virtual News News { get; set; }
[ForeignKey("Post")]
public int PostId { get; set; }
public virtual Post Post { get; set; }
}
public class News
{
[Key]
public int Id { get; set; }
public virtual Picture Picture { get; set; }
}
public class Post
{
[Key]
public int Id { get; set; }
public virtual Picture Picture { get; set; }
}
Related
I have this table using Entity Framework code first approach :
public class WebsitePart
{
public WebsitePart()
{
SubParts = new HashSet<WebsitePart>();
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public Nullable<int> ParentId { get; set; }
[StringLength(100)]
[Index("UQ_WebsitePart_Key", IsUnique = true)]
public string Key { get; set; }
public virtual WebsitePart Parent { get; set; }
public virtual ICollection<WebsitePart> SubParts { get; set; }
}
I need after getting the list of WebsiteParts to map it into List of another model where each element of this list has ParentId=null and down to the end of its grandsons(traversing) .
This the model I want to map to:
public class WebPartViewDto
{
public int Id { get; set; }
public string Key { get; set; }
public IList<WebPartViewDto> SubWebParts { get; set; }
}
Could you please show me how to map it with performance is taken into account?
I'm trying to stay in POCO, but I'm at an impasse:
Public class Alpha()
{
public int ID { get; set; }
[ForeignKey("")]
public int BetaId { get; set; }
public virtual BetaA BetaA{ get; set; }
public virtual BetaB BetaB{ get; set; }
}
Public class BetaA()
{
public int ID { get; set; }
}
Public class BetaB()
{
public int ID { get; set; }
}
How do I get Alpha.BetaId to be both ForeignKey to BetaA.ID and BetaB.ID in POCO?
If anyone else stumble around this
Solution I used
Public class Alpha()
{
public int ID { get; set; }
public int BetaId { get; set; }
[ForeignKey("BetaId")]
public virtual BetaA BetaA{ get; set; }
[ForeignKey("BetaId")]
public virtual BetaB BetaB{ get; set; }
}
Public class BetaA()
{
public int ID { get; set; }
}
Public class BetaB()
{
public int ID { get; set; }
}
have BetaA and BetaB share a primary key then reference BetaB via BetaA
You can also decorate the nav properties with the foreign key attribute and specify the int property as the FK for both, though I have not tested this scenario myself (I'm on a mobile device). Give it a shot and let me know!
This question already has an answer here:
MVC 4 Code First ForeignKeyAttribute on property ... on type ... is not valid
(1 answer)
Closed 6 years ago.
I am using entity framework code first and added a class that has a list of classes of which the classes also have to have another list of classes but when I try to do an update to the database through migrations I get this:
The ForeignKeyAttribute on property 'SubscaleStatistics' on type SubscaleScore' is not valid. The foreign key name 'SubscaleStatisticsId' was not found on the dependent type 'SubscaleScore'. The Name value should be a comma separated list of foreign key property names.
Here's what my classes look like:
public class ExamStatistics : StatisticsData
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
public int TestId { get; set; }
public IList<SubscaleStatistics> Subscales { get; set; }
}
public class SubscaleStatistics
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int SubscaleStatisticsId { get; set; }
public int TestId { get; set; }
public int SubscaleNumber { get; set; }
public int ExamStatisticsId { get; set; }
[ForeignKey("ExamStatisticsId")]
public virtual ExamStatistics ExamStatistics { get; set; }
public IList<SubscaleScore> SubscaleScores { get; set; }
}
public class SubscaleScore
{
public int TestId { get; set; }
public int Subscale { get; set; }
public double Score { get; set; }
public int SubscaleId { get; set; }
[ForeignKey("SubscaleStatisticsId")]
public virtual SubscaleStatistics SubscaleStatistics { get; set; }
}
What am I doing wrong here? Or do I need to provide more information to get what's wrong?
You need to add a foreign key property to SubscaleScore.
public int SubscaleStatisticsId { get; set; }
see here for tutorial: foreign keys
the foreignkey must be a id not the object
try something like this:
public class SubscaleScore
{
public int TestId { get; set; }
public int Subscale { get; set; }
public double Score { get; set; }
public int SubscaleId { get; set; }
[ForeignKey("SubscaleStatisticsId")]
public int SubscaleStatisticsId { get; set; }
public virtual SubscaleStatistics SubscaleStatistics { get; set; }
}
do the same for all classes with foreignkey
I have two tables
PropertyListing - It stores the details of the property user add, with an FK
PropertyAvailability - It's a table that stores property status ( Now Available, After 3 Months, ...)
I am trying to enforce a one-to-many relation with these two tables (Fluent API) like this
public partial class PropertyListing
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
public string StreetAddress { get; set; }
//the column that links with PropertyAvaibility table PK
public byte? Availability { get; set; }
public bool Status { get; set; }
public virtual PropertyAvailability PropertyAvailability { get; set; }
}
public partial class PropertyAvailability
{
public byte ID { get; set; }
public string Status { get; set; }
public virtual ICollection<PropertyListing> PropertyListings { get; set; }
public PropertyAvailability()
{
PropertyListings = new List<PropertyListing>();
}
}
I am calling this on OnModelCreating
modelBuilder.Entity<PropertyListing>()
.HasRequired(pl => pl.PropertyAvailability)
.WithMany(pa => pa.PropertyListings)
.HasForeignKey(pl => pl.Availability);
It fails with this error,
Invalid column name 'PropertyListing_ID'.
Tutorial I used: http://www.entityframeworktutorial.net/code-first/configure-one-to-many-relationship-in-code-first.aspx
What could be wrong? I know I have screwed up the naming convention EF6 expects, but isn't there a workaround?
P.S: I have seen this question asked from ef3 or so in our SO, but I am unable to find any solution and hence the question.
Add the Column attribute to you class
public partial class PropertyListing
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity), Column("ID")]
public int ID { get; set; }
public string StreetAddress { get; set; }
//the column that links with PropertyAvaibility table PK
public byte? Availability { get; set; }
public bool Status { get; set; }
public virtual PropertyAvailability PropertyAvailability { get; set; }
}
My database structure looks like this: Categories table, Questions table and Answers table. Questions have a CategoryID, and Answers have an AnswerID. When trying to access the navigation property "Answers" in my view, I receive the following error:
"'System.Data.Entity.Core.EntityCommandExecutionException' occurred in
EntityFramework.SqlServer.dll but was not handled in user code" Which
is accompanied by:
"Invalid column name 'Question_ID'."
Here's what my classes look like:
public class Category
{
public int ID { get; set; }
public string Text { get; set; }
public string Description { get; set; }
public virtual List<Question> Questions { get; set; }
}
For Question:
public class Question
{
public int ID { get; set; }
public int CategoryID { get; set; }
public string Text { get; set; }
public virtual List<Answer> Answers { get; set; }
public virtual Category Category { get; set; }
}
And for Answer:
public class Answer
{
public int ID { get; set; }
public int QuestionID { get; set; }
public string Text { get; set; }
public int Correct { get; set; }
public virtual Question Question { get; set; }
}
And my Database uses exactly the same field names, with foreign keys applied. I have tried so many variations on the names, including changing the foreign keys to 'Question_ID'and 'Category_ID' but no luck. I've also tried this solution: Code First conventions confusion but it didn't work. Anybody know where I'm going wrong?
I believe the problem is with your answer class. Try this...
public class Answer
{
public int ID { get; set; }
public string Text { get; set; }
public int Correct { get; set; }
[ForeignKey("Question")]
public int QuestionID { get; set; }
public virtual Question Question { get; set; }
}