Foreign key property not valid? (Entity framework code first) [duplicate] - c#

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

Related

Code First Entity Framework - Column returns null becuase EF changed the name to include _ID

Entity framework changed the column name in the DB, and isn't giving me it's value.
Here are my classes:
public class Settings
{
[Key]
public int ID { get; set; }
public string Setting { get; set; }
public string MoreDetail { get; set; }
public SettingTypes Type { get; set; }
public SettingGroups SettingGroup { get; set; }
public int? MinMembership { get; set; }
public string DefaultValue { get; set; }
public int? ParentID { get; set; }
public int Order { get; set; }
}
public class SettingTypes
{
[Key]
public int ID { get; set; }
[MaxLength(35)]
public string TypeName { get; set; }
}
public class SettingGroups
{
[Key]
public int ID { get; set; }
[MaxLength(35)]
public string GroupName { get; set; }
}
In the DB you can see that it changed the name of the two columns:
When I try to loop through the results, type is null:
How do I retrieve this value? I've tried renaming the columns in the class and in the DB but that just breaks more things. What's the proper way to handle this?
Thanks!
Dangit, I figured it out. Spent way to much time doing so.
It was as simple as adding "virtual" to the properties:
public virtual SettingTypes Type { get; set; }
public virtual SettingGroups SettingGroup { get; set; }
Now I can address it like:
setting.Type.TypeName
Hope this saves someone else some time.

I'm having problems setting a foreingKey

I'm trying to create the table "Prodotti" with 2 foreing keys to itself but I keep having this error: "The ForeignKeyAttribute on property 'ProdottiRichiesti' on type 'BugTracking_Entity.Prodotti' is not valid. The navigation property 'Prodotti' was not found on the dependent type 'BugTracking_Entity.Prodotti'. The Name value should be a valid navigation property name."
This is my code:
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[ForeignKey("Prodotti")]
public virtual int ProdottiRichiesti { get; set; }
[ForeignKey("Prodotti")]
public virtual int ProdottiIncompatibili { get; set; }
using System.ComponentModel.DataAnnotations.Schema;
public class Student
{
public int StudentID { get; set; }
public string StudentName { get; set; }
[ForeignKey("Standard")]
public int StandardRefId { get; set; }
public Standard Standard { get; set; }
}
public class Standard
{
public int StandardId { get; set; }
public string StandardName { get; set; }
public ICollection<Student> Students { get; set; }
}
You have to create a Prodotti class instead of Standart like that example.

FK column to mulitple tables POCO

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!

1 to 1 relationship EF two foreign keys error [duplicate]

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; }
}

Entity Framework 6 - foreign key issue

I have the following model using EF6, where I'm trying to link a 'ContentArticleHOAsubdivision' entity to a 'SubdivisionHOA':
public partial class ContentArticleHOAsubdivision
{
public int Id { get; set; }
[ForeignKey("ContentArticleHOA")]
public long ContentArticleId { get; set; }
[ForeignKey("SubdivisionsHOA")]
public short SubdivisionId { get; set; }
public virtual ContentArticleHOA ContentArticleHOA { get; set; }
public virtual ICollection<SubdivisionHOA> SubdivisionsHOA { get; set; }
}
public partial class SubdivisionHOA
{
public short Id { get; set; }
public string Name { get; set; }
[ForeignKey("TopTierDivisionHOA")]
public byte TopTierDivisionId { get; set; }
public virtual TopTierDivisionHOA TopTierDivisionHOA { get; set; }
}
I get the error:
The foreign key component 'SubdivisionId' is not a declared property on type 'SubdivisionHOA'. Verify that it has not been explicitly excluded from the model and that it is a valid primitive property.
The 'Id' property in SubdivisionHOA I'm trying to link on needs to be named 'Id' as that's the name of the actual column in the database, so I'm not sure what I need to do?
I've tried adding [InverseProperty("Id")] before the [ForeignKey] attributes but get the same error.
Basicly the foreign key usage in EntityFramework seems like this:
public partial class ContentArticleHOAsubdivision
{
public int Id { get; set; }
...
public virtual ICollection<SubdivisionHOA> SubdivisionsHOAs { get; set; }
}
public partial class SubdivisionHOA
{
public short Id { get; set; }
...
public int ContentArticleHOAsubdivisionId { get; set; }
[ForeignKey("ContentArticleHOAsubdivisionId")]
public virtual ContentArticleHOAsubdivision ContentArticleHOAsubdivision { get; set; }
}
Just based on your code, and I have discard some unknown stuffs.

Categories

Resources