Somehow the Entity Framework is not given to me. So I determined the models like this:

 [Table("Accounts")] public class Account { [Key] public int Id { get; set; } public string Token { get; set; } public string UserId { get; set; } public string Name { get; set; } public string ImageUrl { get; set; } public virtual ICollection<TaskProto> Tasks { get; set; } } [Table("Groups")] public class Group { [Key] public int Id { get; set; } public string Name { get; set; } public string ImageUrl { get; set; } public virtual ICollection<TaskProto> Tasks { get; set; } } [Table("Tasks")] public class TaskProto { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } public int AccountId; public int GroupId; public virtual Account Account { get; set; } public virtual Group Group { get; set; } public string Name { get; set; } } 

Fluent is:

 modelBuilder.Entity<TaskProto>().HasRequired(p => p.Account).WithMany(b => b.Tasks).HasForeignKey(t => t.AccountId); modelBuilder.Entity<TaskProto>().HasRequired(p => p.Group).WithMany(b => b.Tasks).HasForeignKey(t=>t.GroupId); 

And everything does not work, just the program skips a bunch of lines and draws an incompletely initialized form. It does not give any errors. If you remove HasForeignKey at least something works, naturally dependent objects in TaskProto do not pull up. Already shoveled a bunch of materials, doing everything seems like a textbook. Chyadt?

Up

I tried this:

 [Table("Tasks")] public class TaskProto { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } [Column("Account_Id")] public int AccountId; [Column("Group_Id")] public int GroupId; public virtual Account Account { get; set; } public virtual Group Group { get; set; } public string Name { get; set; } public TaskProto() { } } 
  modelBuilder.Entity<TaskProto>().HasRequired(p => p.Account).WithMany(b => b.Tasks).Map(m => m.MapKey("Account_Id")); modelBuilder.Entity<TaskProto>().HasRequired(p => p.Group).WithMany(b => b.Tasks).Map(m => m.MapKey("Group_Id")); 

Did not help.

  • In step by step debugging, look at what falls then. - Monk
  • With db.Tasks.ToList(); and db.Tasks.Add(tsk); - Sergey
  • Well, it’s already obvious that there is an EF exception, look for it. In the logs, if they do not fall out. - Monk
  • The properties expression 't => t.AccountId' is not valid. The expression should represent a property: C#: 't => t.MyProperty' VB.Net: 'Function(t) t.MyProperty'. When specifying multiple properties use an anonymous type: C#: 't => new { t.MyProperty1, t.MyProperty2 }' VB.Net: 'Function(t) New With { t.MyProperty1, t.MyProperty2 }'.. The properties expression 't => t.AccountId' is not valid. The expression should represent a property: C#: 't => t.MyProperty' VB.Net: 'Function(t) t.MyProperty'. When specifying multiple properties use an anonymous type: C#: 't => new { t.MyProperty1, t.MyProperty2 }' VB.Net: 'Function(t) New With { t.MyProperty1, t.MyProperty2 }'.. - Sergey
  • 2
    You write in plain text - it must be a property. Add {get; set;} for fields that you swear to become properties. - Monk

1 answer 1

As a result, the problem was in {get;set;} for the keys AccountId and GroupId . The simplest inattention on my part. Thanks to everyone who helped.