I have two models (ApplicationStore, StoreAction) as shown below. I am trying to make one to many relationships i.e. ApplicationStore can have many StoreActions. I am using below fluent API syntax to make one to many relationship but getting error
"One or more validation errors were detected during model generation: StoreActions_Store_Target_StoreActions_Store_Source: : The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical."
public class ApplicationStore
{
public int StoreId { get; set; }
public string Name { get; set; }
public bool IsActive { get; set; }
public System.DateTime CreatedAt { get; set; }
public System.DateTime ModifiedAt { get; set; }
public string ModifiedBy { get; set; }
public virtual ICollection<StoreAction> Actions { get; set; }
}
public class StoreAction
{
public int ActionId { get; set; }
public int StoreId { get; set; }
public string ActionName { get; set; }
public string ActionRoute { get; set; }
public string RequestType { get; set; }
public virtual ApplicationStore Store { get; set; }
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<ApplicationStore>().HasKey(m => new { m.StoreId, m.Name });
modelBuilder.Entity<ApplicationStore>().Property(t => t.StoreId).HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity);
modelBuilder.Entity<ApplicationStore>().Property(m => m.Name).HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.None);
modelBuilder.Entity<StoreAction>().HasKey(m => new { m.ActionId });
modelBuilder.Entity<StoreAction>().Property(m => m.ActionId).HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity);
// configures one-to-many relationship
modelBuilder.Entity<ApplicationStore>().HasMany(a => a.Actions).WithRequired(s => s.Store).HasForeignKey(s=> s.StoreId);
base.OnModelCreating(modelBuilder);
}
Note: I have applied solution mentioned in following links Fluent API One to Many mapping table Entity Framework Relationship Fluent Api(One To Many) and many others.