Answer the question
In order to leave comments, you need to log in
ASP .NET MVC Entity Framework what is wrong and how to fix it?
I have the following code in ASP .NET
public class Area // Район
{
[Key]
public int AreaId { get; set; }
public string Name { get; set; }
[ForeignKey("RegionId")]
public Region RegionId { get; set; }
public byte[] Map { get; set; }
}
public class Region // Область
{
[Key]
public int RegionId { get; set; }
public string Name { get; set; }
public byte[] Map { get; set; }
}
public class EFDbContext : DbContext
{
public DbSet<Area> Areas { get; set; }
public DbSet<Region> Regions { get; set; }
}
The ForeignKeyAttribute on property 'RegionId' on type 'UkraineColleges.Domain.Entities.Area' is not valid. The foreign key name 'RegionId' was not found on the dependent type 'UkraineColleges.Domain.Entities.Area'. The Name value should be a comma separated list of foreign key property names.
Answer the question
In order to leave comments, you need to log in
In your case, you can simplify the code and do without specifying attributes:
public class Area // Район
{
public int Id { get; set; }
public string Name { get; set; }
public int RegionId { get; set; }
public virtual Region Region {get; set;}
public byte[] Map { get; set; }
}
public class Region // Область
{
public int Id { get; set; }
public string Name { get; set; }
public byte[] Map { get; set; }
}
public class Area // Район
{
[Key]
public int AreaId { get; set; }
public string Name { get; set; }
public int RegionId { get; set; }
[ForeignKey("RegionId")]
public virtual Region Region {get; set;}
public byte[] Map { get; set; }
}
public class Region // Область
{
[Key]
public int RegionId { get; set; }
public string Name { get; set; }
public byte[] Map { get; set; }
}
[ForeignKey("RegionId")]
public Region RegionId { get; set; }
----------------
[ForeignKey("RegionId")]
public int RegionId { get; set; }
public Region Region { get; set; }
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question