当我尝试在实体框架中插入实体时出现抛出异常错误(使用代码优先)
当我尝试在实体框架中插入实体(使用代码优先)时,出现强制转换异常。
转换异常就像“不可能将...Collection'1(Entity)转换为类型(Entity)”
从这段代码:
public virtual T Insert(T entity)
{
return Context.Set<T>().Add(entity);
}
我不明白为什么。我很确定我做的一切都是正确的。
发布实体
public class Post
{
public long PostId { get; private set; }
public DateTime date { get; set; }
[Required]
public string Subject { get; set; }
public User User { get; set; }
public Category Category { get; set; }
[Required]
public string Body { get; set; }
public virtual ICollection<Tag> Tags { get; private set; }
public Post()
{
Category = new Category();
if (Tags == null)
Tags = new Collection<Tag>();
}
public void AttachTag(string name, User user)
{
if (Tags.Count(x => x.Name == name) == 0)
Tags.Add(new Tag {
Name = name,
User = user
});
else
throw new Exception("Tag with specified name is already attached to this post.");
}
public Tag DeleteTag(string name)
{
Tag tag = Tags.Single(x => x.Name == name);
Tags.Remove(tag);
return tag;
}
public bool HasTags()
{
return (Tags.Count > 0);
}
}
标记实体
public class Tag
{
public long TagId { get; private set; }
public string Name { get; set; }
// Qui a ajouté le tag ?
public User User { get; set; }
}
映射
public class PostMap: EntityTypeConfiguration<Post>
{
public PostMap()
{
ToTable("Posts");
HasKey(x => x.PostId);
Property(x => x.Subject)
.HasColumnType("varchar")
.HasMaxLength(256)
.IsRequired();
Property(x => x.Body)
.HasColumnType("text")
.IsRequired();
HasMany(x => x.Tags);
HasOptional(x => x.Tags);
}
}
class TagMap : EntityTypeConfiguration<Tag>
{
public TagMap()
{
ToTable("Tags");
HasKey(x => x.TagId);
Property(x => x.Name)
.HasColumnType("varchar")
.HasMaxLength(256)
.IsRequired();
HasRequired(x => x.User);
}
}
非常感谢。
I get an cast exception when i am trying to insert an entity in Entity Framework (using code-first).
The cast exception is like "impossible to cast ...Collection'1(Entity) to type (Entity)"
From this code :
public virtual T Insert(T entity)
{
return Context.Set<T>().Add(entity);
}
I can't figure out why. I am pretty sure ive done everything right.
Post entity
public class Post
{
public long PostId { get; private set; }
public DateTime date { get; set; }
[Required]
public string Subject { get; set; }
public User User { get; set; }
public Category Category { get; set; }
[Required]
public string Body { get; set; }
public virtual ICollection<Tag> Tags { get; private set; }
public Post()
{
Category = new Category();
if (Tags == null)
Tags = new Collection<Tag>();
}
public void AttachTag(string name, User user)
{
if (Tags.Count(x => x.Name == name) == 0)
Tags.Add(new Tag {
Name = name,
User = user
});
else
throw new Exception("Tag with specified name is already attached to this post.");
}
public Tag DeleteTag(string name)
{
Tag tag = Tags.Single(x => x.Name == name);
Tags.Remove(tag);
return tag;
}
public bool HasTags()
{
return (Tags.Count > 0);
}
}
Tag entity
public class Tag
{
public long TagId { get; private set; }
public string Name { get; set; }
// Qui a ajouté le tag ?
public User User { get; set; }
}
Mapping
public class PostMap: EntityTypeConfiguration<Post>
{
public PostMap()
{
ToTable("Posts");
HasKey(x => x.PostId);
Property(x => x.Subject)
.HasColumnType("varchar")
.HasMaxLength(256)
.IsRequired();
Property(x => x.Body)
.HasColumnType("text")
.IsRequired();
HasMany(x => x.Tags);
HasOptional(x => x.Tags);
}
}
class TagMap : EntityTypeConfiguration<Tag>
{
public TagMap()
{
ToTable("Tags");
HasKey(x => x.TagId);
Property(x => x.Name)
.HasColumnType("varchar")
.HasMaxLength(256)
.IsRequired();
HasRequired(x => x.User);
}
}
Thanks a lots.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请确保您已将单个元素传递给 Insert 方法,而不是包含单个元素的集合。
Please make sure that you have passed a single element to the Insert method, not a collection containing a single element.
我找到了解决方案。
这里是 Post 的正确映射场景:
指定 Tags 集合是可选非常重要。本场景就是这种情况。帖子可以附加零个标签。
HasMany(x => x.Tags).WithOptional();
I found the solution.
Here the correct mapping scenario for Post :
It is important to specify the the Tags collection is optional. Which is the case in this scenario. A Post can have zero tags attached to it.
HasMany(x => x.Tags).WithOptional();