Entity Framework 6, cannot use Ignore method on the property

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

Question

I'm currently utilizing MVC 5 and ASP Identity. The AspNetUsers table has a phone number field that I wish to remove, but using the add-migration command results in 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've previously read a ton of questions on this site, but every single one of them indicated that you should ignore properties in your base class; however, in my situation, I don't have access to the base.

How can I fix this issue?

Update: Since I didn't want to utilize fluent API in this manner, I created distinct config classes for each object. When using fluent API inside the OnModelCreating method, everything worked.

Here's my code:

Descending Entity Class

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

}  

Setting class

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

Context Type

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

A good starting point is the [NotMapped] attribute from System.ComponentModel.DataAnnotations.Schema. This may help you overcome that restriction because it has been used to ignore Enums in the mapping, albeit it might not be what you were looking for.

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