How to solve EF-Core Code-First table foreign key column missing

entity-framework entity-framework-core

Accepted Answer

To be clear, you don't always require navigation properties, i.e.public City City { get; set; } to establish connections. The foreign key and the appropriate settings are the only things required.

Although I haven't tried it, I believe the following setup might work for you:

Entities

If you don't mind, I also purposefully changed your current courses to adhere to C# Naming Guidelines. Keep in mind that if you're using Code First, you may order your classes in whatever order you choose. Later, perseverance comes to mind. In fact, I'll demonstrate how you may rename attributes of classes when you persist them to your database using Configurations.

public class County
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class City
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class People
{
    public int Id { get; set; }
    public string Name { get; set; }

    public int CityId { get; set; }
    // Optional
    //public City City { get; set; }

    public int CountyId { get; set; }
    // Optional
    //public County County { get; set; }
}

Configurations

To customize how you wish to translate your classes back to the database, you may use Fluent API with settings rather than Data Annotation.

public class CountyConfiguration : IEntityTypeConfiguration<County>
{
    public void Configure(EntityTypeBuilder<County> builder)
    {
        builder.HasKey(x => x.Id);        // Same as using [Key]

        builder.Property(x => x.Id)
            .HasColumnName("cid");        // If you want to rename to "cid"

        builder.Property(x => x.Name)
            .IsRequired()                 // If you want to mark that field required
            .HasColumnName("cname");      // If you want to rename to "cname"

        builder.ToTable("so_county");     // If you want to rename the table
    }
}

public class CityConfiguration : IEntityTypeConfiguration<City>
{
    public void Configure(EntityTypeBuilder<City> builder)
    {
        builder.HasKey(x => x.Id);        // Same as using [Key]

        builder.Property(x => x.Id)
            .HasColumnName("cid");        // If you want to rename to "cid"

        builder.Property(x => x.Name)
            .IsRequired()                 // If you want to mark that field required
            .HasColumnName("cname");      // If you want to rename to "cname"

        builder.ToTable("so_city");       // If you want to rename the table
    }
}

public class PeopleConfiguration : IEntityTypeConfiguration<People>
{
    public void Configure(EntityTypeBuilder<People> builder)
    {
        builder.HasKey(x => x.Id);        // Same as using [Key]

        builder.Property(x => x.Id)
            .HasColumnName("pid");        // If you want to rename to "pid"

        builder.Property(x => x.Name)
            .IsRequired()                 // If you want to mark that field required
            .HasColumnName("pname");      // If you want to rename to "pname"

        // Relationship
        builder.HasOne<County>()          // People has one County
            .WithMany()                   // County has many people
            .HasForeignKey<County>(x => x.CountyId);  // Foreign key is CountyId

        builder.HasOne<City>()            // People has one City
            .WithMany()                   // City has many people
            .HasForeignKey<City>(x => x.CityId);      // Foreign key is CityId

        builder.ToTable("so_people");     // If you want to rename the table
    }
}

Finally, you must use those setups.OnModelCreating :

public class YourDbContext : DbContext
{
    public DbSet<County> Counties { get; set; }
    public DbSet<City> Cities { get; set; }
    public DbSet<People> People { get; set; }

    public YourDbContext(DbContextOptions<YourDbContext> options) : base(options) {}

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        builder.ApplyConfiguration(new CountyConfiguration());
        builder.ApplyConfiguration(new CityConfiguration());
        builder.ApplyConfiguration(new PeopleConfiguration());
    }
}

DISCLAIM: handwritten. Not examined.

2
3/20/2019 8:34:48 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow