0

I have two tables:

User ID Email

Task Email TaskName

The user can have multiple tasks. I need to specify that Email is the foreign key relationship between my task and email table. How can this be done?

Here is my code:

        public MyDataContext() : base("Default")
        {
        }

        public DbSet<User> Users { get; set; }
        public DbSet<Task> Tasks { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<User>()
                .ToTable("User").HasRequired(m => m.Tasks)
                    .WithMany()
                    .HasForeignKey(m => m.Email)
                    .WillCascadeOnDelete(false);
            modelBuilder.Entity<Task>()
                .ToTable("Task");
        }
Joseph Anderson
  • 4,114
  • 4
  • 45
  • 99
  • Did you ever figure this one out? I'm having the same problem [here](https://stackoverflow.com/q/59363602). – InteXX Dec 16 '19 at 21:58
  • I just created the database table and assigned the primary key, using the designer. – Joseph Anderson Dec 19 '19 at 14:51
  • I see. Using the designer, I was able to apply a custom name to my foreign key. Pity. I was hoping for a way to do it with Code First Migrations, using the Fluent API. – InteXX Dec 19 '19 at 16:24

0 Answers0