2011-02-10 2 views
0

Je reçois une exception d'incantation lorsque j'essaie d'insérer une entité dans Entity Framework (en utilisant code-first).Erreur d'exception d'incantation lorsque j'essaie d'insérer une entité dans Entity Framework (en utilisant code-first)

L'exception est coulé comme "impossible de jeter ... Collection'1 (entité) au type (entité)"

A partir de ce code:

public virtual T Insert(T entity) 
{ 
     return Context.Set<T>().Add(entity); 
} 

Je ne peux pas comprendre pourquoi . Je suis certain que j'ai tout fait correctement.

entité post

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

entité Tag

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


} 
    } 

Merci un bon nombre.

Répondre

0

J'ai trouvé la solution.

Voici le scénario de mise en correspondance correcte du message:

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

Il est important de préciser la collection de balises est en option. Ce qui est le cas dans ce scénario. Une publication peut contenir des étiquettes zéro.

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

0

Veuillez vous assurer que vous avez passé un seul élément à la méthode Insert, et non une collection contenant un seul élément.

+0

Je n'ai aucun contrôle à ce sujet. – Rushino

Questions connexes