Entity Framework 6, cannot use Ignore method on the property

asp.net-identity c# entity-framework-6

Question

Currently, I'm using ASP Identity with MVC 5.I want to remove phone number field from the AspNetUsers table, but when I use add-migration command it causes the following error.

You cannot use Ignore method on the property 'PhoneNumber' on type 'Models.DbModel.User' because this type inherits from the type 'Microsoft.AspNet.Identity.EntityFramework.IdentityUser`

I have already read tons of questions on here, but all of them said you have to ignore property in your base class, however, I don't have any access to the base in this case.

How can I solve this problem?

Update: when I used fluent API inside the OnModelCreating method it worked, I don't want to use it this way so I separated the config class for each entity.

Below is my code:

Derived Entity Class

public class User: IdentityUser
{
    public  ICollection<Comment> Comments { get; set; }

}  

Config class

public sealed class UserConfig : EntityTypeConfiguration<User>
{
    public UserConfig()
    {
        ToTable("dbo", "Users");
        Ignore(x => x.PhoneNumber);
        Ignore(x => x.PhoneNumberConfirmed);
    }
}  

Context Class

public class WebsiteContext : IdentityDbContext
{
    public WebsiteContext()
        : base("XYZ")
    {
    }

    public DbSet<Comment> Comment { get; set; }
    //public DbSet<User> User { get; set; }

    public static WebsiteContext Create()
    {
        return new WebsiteContext();
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Configurations.Add(new CommentConfig());
        modelBuilder.Configurations.Add(new UserConfig());
    }
}
1
0
2/26/2018 9:38:19 AM

Popular Answer

Try the [NotMapped] attribute from System.ComponentModel.DataAnnotations.Schema This might get you around that limitation , it has been used to ignore Enums in the mapping, this might not be exactly what you want

0
7/14/2015 1:18:23 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow