使用 Entity Framework 时出现 InvalidCastException
InvalidCastException when using Entity Framework
我正在尝试在对话中保存用户列表 class,但在执行 POST 时收到 InvalidCastException
。
这是我的用户 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;
}
}
这是我的对话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()
{
}
}
这是对话控制器:
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;
}
编辑
Post 声明:
{
"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": "Unable to cast object of type 'System.Collections.Generic.List`1[AcademicAssistant.Models.User]' to type 'AcademicAssistant.Models.User'.",
你的模型有误。由于 Conversation
和 User
之间存在 N:N 关系,因此您需要从 Members
之前的对话中删除 ForeingKey
属性,并且需要添加 ICollection<Conversation> Conversation
属性 在用户 class 中,或者更进一步,根据需要使用 FluentApi
配置关系
我正在尝试在对话中保存用户列表 class,但在执行 POST 时收到 InvalidCastException
。
这是我的用户 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;
}
}
这是我的对话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()
{
}
}
这是对话控制器:
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;
}
编辑 Post 声明:
{
"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": "Unable to cast object of type 'System.Collections.Generic.List`1[AcademicAssistant.Models.User]' to type 'AcademicAssistant.Models.User'.",
你的模型有误。由于 Conversation
和 User
之间存在 N:N 关系,因此您需要从 Members
之前的对话中删除 ForeingKey
属性,并且需要添加 ICollection<Conversation> Conversation
属性 在用户 class 中,或者更进一步,根据需要使用 FluentApi