EF Required to Optional Relationship not using the right columns for FK

86 Views Asked by At

I already read many questions, but still couldn't find a solution to my problem.

I am trying to have something like this I drawed very quickly

enter image description here

First Try:

public class CarMap : EntityTypeConfiguration<Car>
    {
        public CarMap()
        {
            ToTable("Cars", Constants.Schema);
            Property(t => t.EngineId);

            HasRequired(x => x.SPropertyPolicyInsurance).WithMany().HasForeignKey(x => x.SPropertyPolicyInsuranceId);
        }
    }



public class Car : MyBase
    {
        public int EngineId { get; set; }
        public virtual Engine Engine { get; set; }
    }


public class EngineMap : EntityTypeConfiguration<Engine>
    {
        public EngineMap()
        {
            ToTable("Engines", Constants.Schema);

            Property(t => t.MyField1).HasColumnType("nvarchar").HasMaxLength(128).IsRequired();
            Property(t => t.MyField2).HasColumnType("nvarchar").HasMaxLength(128).IsRequired();
            Property(t => t.MyField3).HasColumnType("bit").IsRequired();            
        }
    }


public class Engine : MyBase
    {
        public string MyField1 { get; set; }
        public string MyField2 { get; set; }
        public bool MyField3 { get; set; }

        public virtual Car Car { get; set; }
    }

Using this solution it creates a new column on my Engine table that I don't want...

Second Try:

Changing the CarMap to the following code:

public class CarMap : EntityTypeConfiguration<Car>
    {
        public CarMap()
        {
            ToTable("Cars", Constants.Schema);
            Property(t => t.EngineId);

            HasRequired(x => x.Engine).WithOptional(p => p.Car);
        }
    }

The problem is that it doesn't use the column EngineId to store my FK key values...

Thank you very much for your help anyway...

1

There are 1 best solutions below

0
On

Never mind, it's working now, I cleaned up all my code, deleted extra relationships that were maybe conflicting and now using the following Fluent API relationship it's working properly using only one Column for FK (EngineId).

The resulting code first migrations are the following:

CreateTable(
     "myschema.Engines",
       c => new
       {
          Id = c.Int(nullable: false),
          myfield = c.String(nullable: false, maxLength: 128),
          myfield2 = c.String(nullable: false, maxLength: 128),
          myfield3 = c.Boolean(nullable: false),
       })
  .PrimaryKey(t => t.Id)
  .ForeignKey("basechema.Engines", t => t.Id)
  .Index(t => t.Id);


CreateTable(
     "myschema.Cars",
        c => new
        {
            Id = c.Int(nullable: false),
            EngineId = c.Int(nullable: false),
        })
   .PrimaryKey(t => t.Id)
   .ForeignKey("baseschema.Cars", t => t.Id)
   .ForeignKey("myschema.Engines", t => t.EngineId)
   .Index(t => t.Id)
   .Index(t => t.EngineId);

I hope it helps someone out there ;)