1

I am trying to save a List of Users in a Conversation class but I am getting an InvalidCastException when performing a POST.

This is my User class:

public class User
{
    [Key]
    [DataMember]
    public String Email { get; set; }

    [DataMember]
    public String Password { get; set; }

    [DataMember]
    public Boolean Admin { get; set; }

    public User(String email, String password, Boolean admin)
    {
        Email = email;
        Password = password;
        Admin = admin;
    }
}

And this is my Conversation class:

[DataContract]
public class Conversation
{
    [Key]
    [DataMember]
    public string Key { get; set; }
    [DataMember]
    public string ConversationName { get; set; }
    [DataMember]
    public string Administrator { get; set; }
    [DataMember]
    [ForeignKey("Email")]
    public List<User> Members { get; set; }

    public Conversation(string key, string name, string admin, List<User> members)
    {
        Key = key;
        ConversationName = name;
        Administrator = admin;
        Members = members;
    }

    public Conversation()
    {

    }
}

This is the Conversation controller:

POST:

    [HttpPost]
    public async Task<IHttpActionResult> PostConversation(Conversation convo)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        foreach (var user in convo.Members)
        {
            db.Entry(user).State = EntityState.Unchanged;
        }

        db.Conversations.Add(convo);
        await db.SaveChangesAsync();

        return CreatedAtRoute("DefaultApi", new { name = convo.Key }, convo);
    }

GET:

[HttpGet]
public IQueryable<Conversation> GetConversations()
{
    return db.Conversations;
}

EDIT Post statement:

{
"Key": "123",
"ConversationName": "Test",
"Administrator": "Ken",
"Members": [{"Email": "[email protected]","Password" : "Passw-1", "Admin" : "true"},
            {"Email": "[email protected]","Password" : "Passw-1", "Admin" : "false"}]
}

The error I am getting is:

"ExceptionMessage": "Unable to cast object of type 'System.Collections.Generic.List`1[AcademicAssistant.Models.User]' to type 'AcademicAssistant.Models.User'.",

27
  • Why are you calling db.Conversations.Add twice? Also, do you have any more information about where the error occurs, or if it gives you any details regarding which property is incorrect. Commented Feb 21, 2016 at 23:23
  • You have 3 required parameters on your user constructor (email, password, and admin). Unless you are passing the email, password, and admin on each member in the conversation, it will not be able to construct your user objects on each member. Since I can't see what you are actually posting, I can't be sure, but I doubt you are posting passwords for the users in the conversation. Commented Feb 21, 2016 at 23:29
  • @Steve thank you for pointing that out, that was a mistake! It is the "Members" property that is giving the trouble. I have edited the question to include more information! Commented Feb 21, 2016 at 23:44
  • @randyh22 should have included that originally.. my bad! It is included now though. I am passing a User into the Conversation and so I am passing the password along with the Email and Admin. It will not be used further in the code but I must include it to be able to use the User class. Commented Feb 21, 2016 at 23:52
  • @semiColon I am wondering if it is having trouble converting your admin properties, which are strings ("true" and "false") to boolean as defined in your user class. Commented Feb 21, 2016 at 23:58

1 Answer 1

1

Your model is wrong. As you have a N:N relationship between Conversation and User you need to remove ForeingKey attribute from conversation before Members, and you need to add ICollection<Conversation> Conversation property in User class, or go further and configure the relationship as you need with FluentApi

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.