Entity Framework. Linkages inherited from one basic class



  • Colleague, please.

    To one-or-zero to one-or-zero. There are user manuals, racers and trainers. Baseline: The user can be a racer, maybe not. Maybe Coach, maybe not. The racer may be associated with the portal user or may not be associated. Coach is similar. Created these models and described the connection. Base class Person will be used to store information common to coaches and racers.

    public class User
    {
        public int Id { get; set; }
        public string UserName { get; set; }
    
    public virtual Rider Rider { get; set; }
    public virtual Coach Coach { get; set; }
    

    }

    public class Rider : Person
    {
    public int Id { get; set; }
    public string RiderName { get; set; }
    }

    public class Coach : Person
    {
    public int Id { get; set; }
    public string CoachName { get; set; }
    }

    public class Person
    {
    public virtual User User { get; set; }
    }

    b.Entity<User>()
    .HasOptional(u => u.Rider)
    .WithOptionalDependent(r => r.User)
    .Map(c => c.MapKey("RiderId"));

    b.Entity<User>()
    .HasOptional(u => u.Coach)
    .WithOptionalDependent(r => r.User)
    .Map(c => c.MapKey("CoachId"));

    In creating migration, I get a mistake:

    User: FromRole: NavigationProperty 'User' is not valid.

    Type 'Coach' of FromRole 'User_Coach_Target' in AssociationType 'User_Coach' must exactly match with the type 'Rider' on which this NavigationProperty is declared on.'

    If you remove the inheritance from class Person and add virtual properties User in classes Coach and Riderthat's all right, migration is generated properly. Tell colleagues what the problem is, how it is to be addressed and whether it is worth doing at all?



  • Your problem is that you're trying to convert two parallel connections into one navigational characteristic, and another class. You can't do that.

    Direct rectification of error - create two User In the final class.

    But actually, you're not all right with the data set. Can a racer be a coach? If you can, we get two different objects. Person with the same name, which is weird. If he can't, why in class? User Do you have two different connections?

    In the first case (the racer may be user and coach) it is better to relinquish and deliver. Person in a separate table, making it mandatory:

    public class User
    {
        public virtual Person Person { get; set; }
    }
    

    public class Rider
    {
    public virtual Person Person { get; set; }
    }

    public class Coach
    {
    public virtual Person Person { get; set; }
    }

    public class Person
    {
    public virtual User User { get; set; }
    public virtual Rider Rider { get; set; }
    public virtual Coach Coach { get; set; }
    }

    b.Entity<Person>().HasOptional(p => p.User).WithRequired(u => u.Person);
    b.Entity<Person>().HasOptional(p => p.Rider).WithRequired(r => r.Person);
    b.Entity<Person>().HasOptional(p => p.Coach).WithRequired(c => c.Person);

    Just be more careful with the identifiers "Optional-Required" the connection is usually made on the primary keys, and to that end, all the aideons except what's in the first place. Person shall not be motorized.

    In the second case, I can recommend that we simply combine two fields. User one:

    public class User
    {
    public int Id { get; set; }
    public string UserName { get; set; }

    public virtual Person Person { get; set; }
    

    }




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2