A specified Include path is not valid. The EntityType 'Model.User' does not declare a navigation property with the name 'Connections'

2

I'm following this example to maintain connection and user information in a database.

Then I created 2 tables:

dbo.User:

dbo.Connection:

These2tablesaboveareverysimple.Ijustcreateditasaprimarykey.

SoIcreatedanew"ADO.NET Entity Data Model" item in the "Models" folder.

Model.Context.cs: (DbContext)

public virtual DbSet<Connection> Connections { get; set; }
public virtual DbSet<User> Users { get; set; }

Connection.cs:

public partial class Connection
{
    public string ConnectionID { get; set; }
    public string UserAgent { get; set; }
    public bool Connected { get; set; }
}

User.cs:

public partial class User
{
    public string UserName { get; set; }
    //Adicionei esse novo código abaixo:
    public ICollection<Connection> Connections { get; set; }
}

I get the following error:

Additional information: A specified Include path is not valid. The EntityType 'Model.User' does not declare a navigation property with the name 'Connections'.

Follow the code:

public override Task OnConnected()
{
    var name = Context.User.Identity.Name;
    using (var db = new Entities())
    {
        var user = db.Users.Include(u => u.Connections).SingleOrDefault(u => u.UserName == name);

        if (user == null)
        {
            user = new User
            {
                UserName = name,
                Connections = new List<Connection>()
            };
            db.Users.Add(user);
        }

        user.Connections.Add(new Connection
        {
            ConnectionID = Context.ConnectionId,
            UserAgent = Context.Request.Headers["User-Agent"],
            Connected = true
        });
        db.SaveChanges();
    }
    return base.OnConnected();
}

The problem is occurring on the line: var user = db.Users.Include(u => u.Connections).SingleOrDefault(u => u.UserName == name); .

Any solution?

    
asked by anonymous 14.03.2017 / 07:58

1 answer

1
The navigation property "Connections" should be declared as virtual:

public partial class User
{
    public string UserName { get; set; }
    //Adicionei esse novo código abaixo:
    public virtual ICollection<Connection> Connections { get; set; }
}
    
14.03.2017 / 12:45