3
I’m following this example to maintain connection and user information in a database.
So I created 2 tables:
dbo. User:
dbo. Connection:
These 2 tables above are very simple. I only created as a primary key.
Then I created a new "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'.
Follows 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);
.
Some solution ?
Has an answer here that can help you understand why the problem.
– Marconi