Reputation: 71
I have this class....
public class ApplicationUser : IdentityUser
{
[Required]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
public virtual ICollection<Post> PostsCreated { get; set; }
public virtual ICollection<Post> PostsModified { get; set; }
public virtual Comment Comment { get; set; }
}
And the other class is this...
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Body { get; set; }
public virtual ICollection<Category> Category { get; set; }
public DateTime PublishDate { get; set; }
public DateTime CreateDate { get; set; }
//----------------------------------------------------------------
[ForeignKey("UserCreated"), Column(Order = 0)]
public string CreatedId { get; set; }
[ForeignKey("UserModify"), Column(Order = 1)]
public string ModifiedId { get; set; }
//----------------------------------------------------------------
[InverseProperty("PostsCreated")]
public virtual ICollection<ApplicationUser> UserCreated { get; set; }
[InverseProperty("PostsModified")]
public virtual ICollection<ApplicationUser> UserModify { get; set; }
//----------------------------------------------------------------
public DateTime ModifiedDate { get; set; }
public Public Public { get; set; }
}
My data acces is this.....
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("BlogLibrary2")
{
}
public DbSet<Post> Post { get; set; }
public DbSet<Comment> Comment { get; set; }
public DbSet<Category> Category { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
}
}
And I got this error when I try to create a controller using this data acces and the Post class:
There was an error running the selected code generator: 'Unable to retrieve metadata for 'LibraryBlog.Models.Post'. The foreign key component 'CreatedId' is not a declared property on type 'ApplicationUser'.Verify that it has not been explicitly excluded from the model and that it is a valid primitive property.'
Upvotes: 1
Views: 248
Reputation: 71
I fixed that error. I modified this class and now look like this:
public class ApplicationUser : IdentityUser
{
[Required]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
public virtual ICollection<Post> PostsCreated { get; set; }
public virtual ICollection<Post> PostsModified { get; set; }
public virtual Comment Comment { get; set; }
}
And this one:
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Body { get; set; }
public virtual ICollection<Category> Category { get; set; }
public DateTime PublishDate { get; set; }
public DateTime CreateDate { get; set; }
//---------------------Here is the point
public string PostCreatedBy { get; set; }
public string PostModifiedBy { get; set; }
[ForeignKey("PostCreatedBy"), InverseProperty("PostsCreated")]
public virtual ApplicationUser CreatedBy { get; set; }
[ForeignKey("PostModifiedBy"), InverseProperty("PostsModified")]
public virtual ApplicationUser ModifiedBy { get; set; }
//-------------------------------------------
public DateTime ModifiedDate { get; set; }
public Public Public { get; set; }
}
Upvotes: 2
Reputation: 1046
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("BlogLibrary2")
{
}
public DbSet<Post> posts { get; set; }
public DbSet<Comment> comments { get; set; }
public DbSet<Category> categories { get; set; }
}
Would you please edit the code with the above one. Hope this helps..
Upvotes: 0