当我尝试在实体框架中插入实体时出现抛出异常错误(使用代码优先)

发布于 2024-10-16 20:46:31 字数 2579 浏览 4 评论 0原文

当我尝试在实体框架中插入实体(使用代码优先)时,出现强制转换异常。

转换异常就像“不可能将...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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

打小就很酷 2024-10-23 20:46:31

请确保您已将单个元素传递给 Insert 方法,而不是包含单个元素的集合。

Please make sure that you have passed a single element to the Insert method, not a collection containing a single element.

無處可尋 2024-10-23 20:46:31

我找到了解决方案。

这里是 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();
            HasRequired(x => x.User);
            HasMany(x => x.Tags).WithOptional();
        }

指定 Tags 集合是可选非常重要。本场景就是这种情况。帖子可以附加零个标签。

HasMany(x => x.Tags).WithOptional();

I found the solution.

Here the correct mapping scenario for 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();
            HasRequired(x => x.User);
            HasMany(x => x.Tags).WithOptional();
        }

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文