A specified Include path is not Valid. The Entitytype 'Model.User' does not declare a navigation Property with the name 'Connections'

Asked

Viewed 828 times

3

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

So I created 2 tables:

dbo. User:

inserir a descrição da imagem aqui

dbo. Connection:

inserir a descrição da imagem aqui

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.

1 answer

1

To 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; }
}
  • Marcell Alves, it didn’t work, it’s still a problem.

  • Marcell Alves, I managed to solve problem, I forgot to put relationships between 2 tables. Now the problem is occurring on the line : db.SaveChanges(); Error: Additional information: Validation failed for one or more entities. See 'Entityvalidationerrors' Property for more Details.

  • 1

    And which message is being returned by the 'Entity Validationerrors' property'?

  • Here it is {System.Data.Entity.Validation.DbEntityValidationResult}

  • 1

    In fact, we need the content of this message, which will accurately indicate the error that is occurring. Use the Visual Studio "Viewdetail" option when the error occurs and navigate inside the properties of the raised exception to find the text of the contents of that property.

  • 1

    https://s8.postimg.org/q4t2h94x1/52_Mar_14_15_04.jpg

  • 1

    To see the real value of this property, you will need to use the "Quickwatch" of Visual Studio, follow a link describing how: http://stackoverflow.com/a/33264768/3090945

  • Now it shows why this problem: ErrorMessage "O campo UserAgent deve ser uma cadeia de caracteres ou tipo de matriz com comprimento máximo de '50'."

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.