T
T
TeVeTed2016-05-31 00:30:30
ASP.NET
TeVeTed, 2016-05-31 00:30:30

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

I have an error while compiling
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.

There is a ready-made database in the App_Data folder, I'm trying to connect it.
I'm working with Entity Framework for the first time, I used to use ASP .Net, but instead of the database there was a manually created data store.
Before that, there were errors too, but rereading the EF sources, I replaced some errors with others, but here I can’t find a replacement anymore ...
Help who knows and if you can, give a couple of tips with the Code-First model, who worked with EF

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sanostee, 2016-05-31
@TeVeTed

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

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

Read more
www.entityframeworktutorial.net/code-first/foreign...
https://msdn.microsoft.com/ru-ru/data/gg193958.aspx
andrey.moveax.ru/post/mvc3-in-depth-entity- framew...

#
#algooptimize #bottize, 2016-05-31
@user004

[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 question

Ask a Question

731 491 924 answers to any question