使用Entity Framework时出现InvalidCastException [英] InvalidCastException when using Entity Framework

查看:162
本文介绍了使用Entity Framework时出现InvalidCastException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Conversation类中保存用户列表,但在执行POST时我得到 InvalidCastException

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

这是我的用户类:

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;
    }
}

这是我的对话课:

[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()
    {

    }
}

这是对话控制器:

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);
    }

获取:

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

编辑
发表声明:

EDIT Post statement:

{
"Key": "123",
"ConversationName": "Test",
"Administrator": "Ken",
"Members": [{"Email": "y@12.com","Password" : "Passw-1", "Admin" : "true"},
            {"Email": "y@123.com","Password" : "Passw-1", "Admin" : "false"}]
}

我得到的错误是:


ExceptionMessage:无法将类型为'System.Collections.Generic.List`1 [AcademicAssistant.Models.User]'的对象转换为'AcademicAssistant.Models.User'。,

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


推荐答案

你的模型错了。由于你在对话用户之间有N:N关系,你需要删除 ForeingKey 来自成员之前的对话的属性,您需要添加 ICollection< Conversation>用户类中的对话属性,或者根据需要使用FluentApi进一步配置关系

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

这篇关于使用Entity Framework时出现InvalidCastException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆