Mapping an existing table using Entity Framework

I am running into some difficulty when the attemp maps my class to an existing table using the Entity Framework.

My class:

[Table("builder_User")]
public class MobileUser
{
    [Key]
    [Column("id")]
    public int Id { get; set; }
    [Column("beansCount")]
    public int BeansCount { get; set; }
    [Column("bonusSum")]
    public double BonusSum { get; set; }
    [Column("facebookUsername")]
    public string FacebookUserName { get; set; }
    [Column("firstName")]
    public string FirstName { get; set; }
    [Column("lastName")]
    public string LastName { get; set; }
    [Column("guid")]
    public string Guid { get; set; }
    [Column("job")]
    public string Job { get; set; }
    [Column("purchasedSum")]
    public double PurchasedSum { get; set; }
    [Column("facebookId")]
    public string FacebookId { get; set; }

}

      

My table in the database

enter image description here

And in the class DataContext

I have:

public DbSet<MobileUser> MobileUsers { get; set; }

      

But when I try to get users from the database, I get the exception

The DbContext has changed since the database was created ....

When I run the command add-migration

, it generates the create table command.

So what's my mistake? Thanks to

+3


source to share


1 answer


Set the initializer to null by calling Database.SetInitializer<TContext>(null)

orDatabase.SetInitializer(new NullDatabaseInitializer<TContext>()



By setting the initializer to null, EF will no longer check the database schema and will no longer create the database if it changes. You will need to create / update the database yourself after changing the schema.

+4


source







All Articles